Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5379
  • Last Modified:

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.
0
snowdog_2112
Asked:
snowdog_2112
  • 5
  • 5
2 Solutions
 
snickeredCommented:
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
0
 
snowdog_2112Author Commented:
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!
0
 
snickeredCommented:
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?
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
snowdog_2112Author Commented:
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"
0
 
snickeredCommented:
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...
0
 
snowdog_2112Author Commented:
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.
0
 
snickeredCommented:
Your post-job command should create and then run the schtask on-the-fly.  AFAIK you can't pass parameters like you want to.
0
 
snowdog_2112Author Commented:
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!
0
 
snickeredCommented:
There's a /change parameter that you can use.  Or simply /delete then /create each time.
0
 
snowdog_2112Author Commented:
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.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now