powershell and scheduled tasks with parameters

I often use “scheduled tasks” to run powershell scripts.  Routinely, i have to fuss with the setup.  I use the path of the powershell.exe as the task and “-file “C:\scripts\stuff\script.ps1” as the argument.

Resently, i wanted to pass a parameter to the script.  It was not dynamic and would change each semester (time unit).  I COULD have put it into a “.bat” file and point to the batch file from the scheduled task.  I tried to just append the parameter as part of the argument to -file, but immediately the task would not run.

The solution seems NOT to use -file, but use -command and use “&” to evaluate the script. We can then pass a parameter as a second argument to the -command.  Like this

-command ” &  ‘c:\scripts\stuff\script.ps1’ ‘parameter_here’ ”

see http://stackoverflow.com/questions/1315140/powershell-2-0-param-keyword-error

It is n0t going to calculate the ‘parameter_here” value for us, but i can change the value externally without messing with the ps script and possibly breaking the scheduled task.

This entry was posted in Uncategorized. Bookmark the permalink.