I am trying to string together a Schtasks command (for task scheduler) in powershell.
Through multiple variables, I successfully create the following:
$ScheduleAdd = "Schtasks /create /tn " + $userAlias +"-Covering-" + $identityAlias + " /sc ONCE /SD " + $single_StartDate + " /st 00:01 " + "/tr " + $([char]34) + "Powershell -command " + $addScript + $([char]34)
if I run "Write-Host $ScheduleAdd" after it in the script, it will successfully write to the console the exact command that can be copied and pasted into command prompt and run to create the scheduled task.
So I'm trying to figure out, why is it not actually adding the task to task scheduler when I set the variable above?
I can copy/paste it into the prompt and run it, and it adds, but if I don't copy paste the command, it will not add?
So Write-Host $ScheduleAdd will print out just like this:
Schtasks /create /tn TSmith-Covering-BJones /sc ONCE /SD 11-24-2014 /st 00:01 /tr "Powershell -command \\server\t$\schedules\Add-
It will print that out, but Powershell isn't executing
it. I have to copy and paste it into the powershell window, and then it executes it, adding it to the scheduled tasks.