I am learning how to pass arguments to external commands in Powershell. I decided to settle on schtasks.exe as a scenario to play with.
What I have decided to do as an exercise to learn is to create functions to getting scheduledtasks and creating scheduledtasks.
During my creation of the function, I hit a bit of a snag. I want to parameterize the arguments and pass that to schtasks.exe.
So I tried this from the schtasks.exe based of an example from schtasks.exe
SCHTASKS /Create /S ABC /U user /P password /RU runasuser /RP runaspassword /SC WEEKLY /TN report /TR notepad.exe
and parameterize it like so:
$comp = "server1" $Server = "/S $comp" $schedule = " /SC WEEKLY" $taskname = "/TN report" $taskrun = "/TR notepad.exe" SCHTASKS "/Create $server $schedule $taskname $taskrun"
It totally bombs with:
SCHTASKS : ERROR: Invalid argument/option - '/s win2k12-dc1'. At line:6 char:1 + SCHTASKS /Create $server $schedule $taskname $taskrun + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (ERROR: Invalid ...s win2k12-dc1'.:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError Type "SCHTASKS /CREATE /?" for usage.
I saw an example on the internet:
$arg1 = "filename1" $arg2 = "-someswitch" $arg3 = "C:\documents and settings\user\desktop\some other file.txt" $arg4 = "-yetanotherswitch" $allArgs = @($arg1, $arg2, $arg3, $arg4) & "C:\Program Files\someapp\somecmd.exe" $allArgs
My question is how does it know what arguments apply to what in schtasks? Or is there a better way to pass arguments to the external command in powershell? Thanks in advance for any help.