Avatar of Garry Shape
Garry Shape
Flag for United States of America

asked on 

Powershell - concatenated command will not run

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)
	Write-Host $ScheduleAdd

Open in new window


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-TSmith-Covering-BJones-11-24-2014.ps1"

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.
PowershellMicrosoft DOS

Avatar of undefined
Last Comment
Garry Shape

8/22/2022 - Mon