3

how can i run schtasks from command line such that i can pass in parameters during run time. i have a batch job which expects parameters during run-time that is created in scheduled task. i cannot have a fixed parameter so i cannot provider the parameter when i first create the task i need to provide this during the run time. i was hoping if there is a way to do like, i need to run this task from schtasks using command line, because i can set the task to run as Admin and give user with less role invoke the task on demand

schtasks /Run /Tn "task" /parameter....?

Natasha Thapa
  • 171
  • 3
  • 9

3 Answers3

3

Here is an example of parameter passing with schtasks:

schtasks /create /tn "test" /TR "\"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe\"-R" /sc daily /st 08:00:00
jscott
  • 24,484
  • 8
  • 79
  • 100
user235457
  • 31
  • 2
  • Is the parameter -R or \-R or "-R"? Is the backslash the thing that tells schtasks there is a parameter coming? Can you have multiple parameters? – Jonathan Elkins Oct 21 '22 at 00:12
1

No - parameters are configured within the task itself.

I'd hesitate to think that this would be workable from a security perspective, either - hypothetically, someone with a task running cmd.exe with a certain set of parameters, having those parameters modified at execution by a non-admin user? Not a good situation.

Shane Madden
  • 114,520
  • 13
  • 181
  • 251
1

Another solution could be to have your powershell script print out the parameters or data that your batch job needs to a text-file, and then have the batch job read from the text file at run time.

I'd take Shane Madden's comment into account though, whoever has access to writing to this text file and executing the scheduled task, has access to a modify the behavior of the process

Mathias R. Jessen
  • 25,161
  • 4
  • 63
  • 95