Link to home
Start Free TrialLog in
Avatar of snowdog_2112
snowdog_2112Flag for United States of America

asked on

schtasks run task with parameters

How can I run a scheduled tasks with parameters?

I have a scheduled task which runs a batch file.  I need to pass parameters to the batch file - but they would need to be passed using the scheduled task.

For example: schtasks /run /tn MyTask "parameter1" "parameter2"

It doesn't run as shown above - but that's the gist of what I am trying to do.
Avatar of snickered
snickered
Flag of United States of America image

Open a cmd window.

This will create a simple batfile for demo purposes:
echo @echo argument is: %1 ^&^& pause > C:\test.bat

Open in new window

Now create the schtask.  The trick is to just use quotes, like so:
schtasks /create /tn TestSchtask /tr "C:\test.bat argument1" /sc daily

Open in new window

Now just run the task.  I added the 'pause' above so you can see the output:
schtasks /run /tn TestSchtask

Open in new window


You can also see that the arguments when in the
schtask.JPG
Avatar of snowdog_2112

ASKER

That's what I was getting as well.  What I need, however, is to pass the value of argument1 from your example.

More specifically, my scheduled task needs to run a batch file with a folder name as the argument.  The folder name is specified when the task is run.

In other words, the "argument1" needs to be dynamic.  Thanks!
You can't do that with only schtasks.  How are the arguments generated?  Maybe there's another way.  Since you're creating the arguments dynamically, why not create the scheduled task dynamically with your arguments included?
the parameter is specified in a post-job command from another process - the post-job command calls a batch file, which is supposed to run schtasks with the arguments I need.  The scheduled task runs RoboCopy with the parameters I am passing.

I have to do it this way because if I run RoboCopy in this other process, it times out and fails my process.  If I have a scheduled task run RoboCopy in another batch file, the scheduled task finishes right away and allows my other process to complete with a success.

It sounds like I am going to be forced to create a pre-defined scheduled task for each possible parameter, and then have a separate batch file for each "schtasks /run /tn run-parm1"
I don't follow this:
the parameter is specified in a post-job command from another process

So, you have one schtask that completes giving you a parameter that you need to use for another schtask?

Can you give the code you're trying to work with and what you're trying to do?  Surely there's a way to do this...
I have several backup processes which have a post-job command option.

Each post-job command needs to run my Scheduled Task, but with a different parameter depending on which post-job command is launching the task.

There's not "code" per se, I'm simply trying to launch a scheduled task with a parameter specified at the time it's launched.

Refering to my OP, the "parameter1", "parameter2" are part of the post-job command from the backup processes.  The scheduled task runs a batch file called "copyfiles.bat", with the source and destination being passed as parameters (e.g., "sourcefolder1", "destfolder1").

Simplified, it's "robocopy %1 %2" (it does more than this, but you get the idea).

I have several backup jobs that will call the schtasks, but each with a different sourcefolder and destfolder. (I can't call the robocopy batch file directly because it takes too long to run and causes the source application to error out - hence the use of schtasks as an intermediary).

Example:
backup job #1 source: files1; destination: \\server\share\folder1
backup job #2 source: files2; destination: \\server\share\folder2

So, I'm looking to have the post-job for each backup job run a single scheduled task and simply pass through the parameters to the batch file.
ASKER CERTIFIED SOLUTION
Avatar of snickered
snickered
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I create the schtasks job on the fly, won't I end up with 100's of tasks (a new task created each time one of the jobs runs)?

Or if the TN is the same, will it "edit" the existing task if one already exists?

Thanks!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Seems like the new Scheduled tasks is "that close" to being really functional.  THe "Parameters" on a task are static, and cannot be passed in using schtasks.exe.

Thanks for the input.
Avatar of Nick Shaw

This wasn't answered correctly, so for anyone else looking for the solution, here it is:  


Wrap the entire command you want to run from the scheduled task (including both executable file/path and any command line arguments/parameters passed to the executable/batch file) in double quotes, BUT your executable file path within those double quotes should be wrapped in single quotes (in order to allow spaces in the file/path name, while differentiating that from the arguments you want to pass to that executable.)  


example:  

schtasks /create /sc WEEKLY /tn "My Task" /tr "'C:\Program Files (x86)\Company Name\Application Name\Some Executable.exe' /update /silent" /ru system /d FRI /st 11:00 /rl HIGHEST /f

Open in new window

So in the example above, the single quotes are wrapped around the path/executable like this:  'C:\Program Files (x86)\Company Name\Application Name\Some Executable.exe' and following that are two command-line arguments /update /silent (which will be passed to Some Executable.exe) and this entire string is wrapped in the double quotes " " to indicate it is all part of the same parameter for the /tr argument of schtasks.


hope that helps anyone else looking for the solution.