locked
how to programmatically pass parameters to an schtasks job

    Question

  • Namaste!

    I've run into a pickle with this.

    I can programmatically create an schtask job without trouble, EXCEPT, it wouldn't run.  That was due to where the exe is stored.

    So, if my exe is store thus:

         C:\Program Files\A Company\The Program\TheProgram.exe

    This needs to be stipulated in scheduler thus:

         "C:\Program Files\A Company\The Program\TheProgram.exe"

    Now, the trick is how to pass a parm to that program.  When the job is programmatically created, I've stipulated the exe path + parm thus:

         "C:\Program Files\A Company\The Program\TheProgram.exe a"

    The little 'a' is the parm.  This creates the job, but it won't run.  Because...

    Scheduler doesn't like that, it is expecting:
      
         "C:\Program Files\A Company\The Program\TheProgram.exe" a 

    The ending quote needs to be around the path/exe and not include the a.

    How do I pass schtasks the path/exe quoted and yet include the parm?  I've tried several variants with no luck - either an error occurs (such as invalid parameter a) or the job is miscreated.

    Thanks!

    Peace, Love, and Light,

    /s/ Jon C. Munson II
    Monday, June 22, 2009 2:31 AM

Answers

  • Namaste!

    I did try to emulate the response in that thread, but, didn't work out for me.

    Rather, putting two-and-two together, I did create a batch file and had the scheduler run that instead.  The trick to it all, at least for me, was to make it such that quotes weren't needed within the application exe call in the TR parameter in the call to schtasks.  Still had to provide quotes around the whole of the TR parameter though.

    Thus, the command-line for schtasks would be (sample only):

        schtasks.exe /create /s \\system /u user /p pwd /sc HOURLY /tn TaskName /tr "8CHARBAT.BAT" /ST 00:00:00 /SD 01/01/1900

    That created a task in the %systemroot%\system32 directory that would run without issue.  The %systemroot%\system32 also proved helpful in clearing this up.

    There were two other "gotchas" though, the Start-In and also the quotes issue.

    In the bat file I had to put a commands in to change to the right drive and then the right directory for the app.  This would then shell the app from that directory (which overrides the Start In).  Then, on the line to run the app, had to enclose that in quotes to be sure it ran.  This may not affect everyone reading this thread, but is/was a consideration for my app.

    Putting things in a bat file wasn't simpler, but, it was just as effective and solved the issue.  Still a kludge in my mind.

    There should be a direct API for this!!!

    Thanks!

    Peace, Love, and Light,

    /s/ Jon C. Munson II

    Tuesday, June 23, 2009 7:24 PM
  • I just wanted to take a moment and update this thread.

    I found a way to pass quotes to schtasks.  It is rather easy (a slap-the-forehead moment indeed).  This was documented in the schtasks /create /? help output on Vista, not on XP.  One can programmatically pass quotes (which accomodate spaces in file/path names) by using escaped quotes (this is one method) thus:

    schtasks.exe /create ... /TR "\"C:\Documents and Settings\user\My Documents\Some Folder\MyProg.exe\""

    Then, a parameter or parameters can be passed without issue thus:

    schtasks.exe /create ... /TR "\"C:\Documents and Settings\user\My Documents\Some Folder\MyProg.exe\" parm"

    This way no bat file and associated baggage is needed.

    Hope this helps!

    Peace, Love and Light,

    /s/ Jon C. Munson II

    P.S.  I still think there ought to be a direct API to expose the Task/JOB as an object proper instead of relying on the schtasks.exe.
    Monday, June 29, 2009 4:16 PM

All replies

  • Part two...

    After further testing, the basic issue is how to pass quotes so that a call to schtasks.exe will keep them.  A sample command line would be:

         schtasks.exe /CREATE /S \\system /U user /P pwd /SC HOURLY /TN TaskName /TR "C:\Documents and Settings\user\My Documents\Visual Studio 2008\Projects\Project\Solution\bin\Debug\project.exe h" /ST 13:13:00 /SD 06/18/2009

    How do I pass quotes so that the exe name is enclosed by them when one examines the task in scheduler?

    Thanks!

    Peace, Love, and Light,

    /s/ Jon C. Munson II
    Monday, June 22, 2009 2:50 AM
  • Hi,

    You might want to try as the thread suggested:
    http://www.tek-tips.com/viewthread.cfm?qid=1538088&page=9

    I 'd suggest you put the command into a bat file , it seems simpler.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, June 23, 2009 3:24 AM
  • Namaste!

    I did try to emulate the response in that thread, but, didn't work out for me.

    Rather, putting two-and-two together, I did create a batch file and had the scheduler run that instead.  The trick to it all, at least for me, was to make it such that quotes weren't needed within the application exe call in the TR parameter in the call to schtasks.  Still had to provide quotes around the whole of the TR parameter though.

    Thus, the command-line for schtasks would be (sample only):

        schtasks.exe /create /s \\system /u user /p pwd /sc HOURLY /tn TaskName /tr "8CHARBAT.BAT" /ST 00:00:00 /SD 01/01/1900

    That created a task in the %systemroot%\system32 directory that would run without issue.  The %systemroot%\system32 also proved helpful in clearing this up.

    There were two other "gotchas" though, the Start-In and also the quotes issue.

    In the bat file I had to put a commands in to change to the right drive and then the right directory for the app.  This would then shell the app from that directory (which overrides the Start In).  Then, on the line to run the app, had to enclose that in quotes to be sure it ran.  This may not affect everyone reading this thread, but is/was a consideration for my app.

    Putting things in a bat file wasn't simpler, but, it was just as effective and solved the issue.  Still a kludge in my mind.

    There should be a direct API for this!!!

    Thanks!

    Peace, Love, and Light,

    /s/ Jon C. Munson II

    Tuesday, June 23, 2009 7:24 PM
  • Glad hear that you can use bat file.

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Wednesday, June 24, 2009 1:25 AM
  • I just wanted to take a moment and update this thread.

    I found a way to pass quotes to schtasks.  It is rather easy (a slap-the-forehead moment indeed).  This was documented in the schtasks /create /? help output on Vista, not on XP.  One can programmatically pass quotes (which accomodate spaces in file/path names) by using escaped quotes (this is one method) thus:

    schtasks.exe /create ... /TR "\"C:\Documents and Settings\user\My Documents\Some Folder\MyProg.exe\""

    Then, a parameter or parameters can be passed without issue thus:

    schtasks.exe /create ... /TR "\"C:\Documents and Settings\user\My Documents\Some Folder\MyProg.exe\" parm"

    This way no bat file and associated baggage is needed.

    Hope this helps!

    Peace, Love and Light,

    /s/ Jon C. Munson II

    P.S.  I still think there ought to be a direct API to expose the Task/JOB as an object proper instead of relying on the schtasks.exe.
    Monday, June 29, 2009 4:16 PM