2

The following script schedules successfully but will not actually, you know, restart the server.

schtasks /create /tn "restart test" /ru "domain\admin" /rp supersecret /sc once /st 11:35 /tr "PowerShell -command {Restart-Computer -ComputerName server01 -force -wait Send-MailMessage -From mail1@domain.com -To mail2@domain.com -Subject 'Rebooted' -SmtpServer smtp.domain.com}"

The Task Scheduler event logs show the task completed successfully:

Task Scheduler successfully completed task "\restart test" , instance "{4a8b0e75-eb2f-4be6-918d-7b66473dd344}" , action "C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.EXE" with return code 0.

If I run just the commands within {...}, it will restart the server and send the notification email.

What am I missing?

EDIT: I am executing this from a Windows 7 laptop with PowerShell 3.0 to a Windows 2012 Server

MadHatter
  • 79,770
  • 20
  • 184
  • 232
Kernel Panic
  • 291
  • 2
  • 8
  • 19
  • @RyanRies not sure I understand what you mean by taking `-Command {..}` out. Did you just take out those items, or everything within them as well? If the latter, what does `schtasks` execute then? I am able to run the powershell cmdlets from the shell. Just not as a task. I probably don't understand what you're trying to say. – Kernel Panic Sep 17 '13 at 19:25
  • @RyanRies I'm sorry. I am sure I am missing something. I still don't understand what you mean. Not sure how sending the commands to a file is relevant. I am not good with the scripting, so I must be missing something simple. – Kernel Panic Sep 17 '13 at 20:11
  • Deleted my previous comments and consolidated them into an answer. – Ryan Ries Sep 17 '13 at 21:17

2 Answers2

2

Looks like /tr isn't parsing the command and the arguments correctly and there doesn't seem to be a reliable way to pass arguments to the command using schtasks.exe. Instead put the PowerShell -command {...} stuff in a batch file (or a *.ps1 file maybe) and invoke it from the task, e.g.

schtasks /create ... /tr "RestartAndSendEmail.cmd"
Duncan Smart
  • 330
  • 2
  • 8
  • I tried as you suggested. I placed the cmdlets into a *.ps1 script and called that from the task. The results are the same. – Kernel Panic Sep 17 '13 at 19:27
  • Your initial suggestion was correct. I obviously mistyped something when I tried it. @Ryan, above, dragged me to the conclusion. Thank you. – Kernel Panic Sep 17 '13 at 21:51
2

OK, I'm deleting my earlier comments, and moving my comments into an answer, since at first my comments started as just general advice, but they turned into heavy clarification with an exact answer.

So I tested with Win7 and PS3, so I'm using the same platform as you.

Firstly, just remove the -Command { ... } from your command, and by that I mean this:

schtasks /create /tn "restart test" /ru "domain\admin" /rp supersecret /sc once /st 11:35 /tr "PowerShell Restart-Computer -ComputerName server01 -force -wait" 

That works. But your next problem is that you want to execute multiple commands, (1 restart the computer and 2 send an email,) and for that I would recommend putting the commands in a ps1 script file and instead making the task like this:

schtasks /create /tn "restart test" /ru "domain\admin" /rp supersecret /sc once /st 11:35 /tr "PowerShell C:\Scripts\restartAndEmail.ps1"

I'm not saying you couldn't technically get two Powershell commands to run in a single call to schtasks, but it's unwieldy and you have to start worrying about characters that need to be escaped on the command line such as the | character, etc., and it just becomes easier from almost every perspective to just use a ps1 file instead.

It's important that you specify the fully-qualified path to the script. The current working directory of the scheduled task will probably not be the same as the path of the script, so relative path won't cut it.

In fact, when I'm making scheduled tasks for Powershell scripts, I typically include the full path to the Powershell exe as well, just to be sure. Those we have already demonstrated here that you don't have to include the fully qualified path to Powershell.exe in this scenario.

Ryan Ries
  • 55,481
  • 10
  • 142
  • 199
  • Thank you. @Duncan, below, suggested the same earlier. I obviously missed something when I tried it after his suggestion. Not sure what I was missing. Apologies for the obtuseness. – Kernel Panic Sep 17 '13 at 21:50