Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a VBScript to do a weekly reboot of all machines on our network. I want to run this script via Task Scheduler. The script runs at 3:00 AM, but there is a small chance that users may still be on the network at that time, and I need to give them the option to terminate the reboot. If they do so, I would like the reboot to occur the next night at 3:00 AM. I've set Task Scheduler up to repeat in this way.

So far, so good. The problem is that if the user selects "Cancel" in my script, the Task Scheduler does not see my task as failed, and won't run it again the next night.

Any ideas? Can I pass an errorcode to task scheduler or otherwise abort the task via VBScript?

My code is below:

Option Explicit
Dim objShell, intShutdown
Dim strShutdown, strAbort

' -r = restart, -t 600 = 10 minutes, -f = force programs to close
strShutdown = "shutdown.exe -r -t 600 -f"
set objShell = CreateObject("WScript.Shell")
objShell.Run strShutdown, 0, false

'go to sleep so message box appears on top
WScript.Sleep 100

' Input Box to abort shutdown
intShutdown = (MsgBox("Computer will restart in 10 minutes. Do you want to cancel computer     restart?",vbYesNo+vbExclamation+vbApplicationModal,"Cancel Restart"))
If intShutdown = vbYes Then
' Abort Shutdown
strAbort = "shutdown.exe -a"
set objShell = CreateObject("WScript.Shell")
objShell.Run strAbort, 0, false
End if


Appreciate any thoughts.

share|improve this question
Related: How does Windows Task Scheduler in Win7 recognize a failed task? No accepted answer, though... – Indrek Jun 21 '12 at 15:33
up vote 3 down vote accepted

Run the script each night, not just once a week. First, check the computer uptime. If the uptime is greater than 7 days, reboot the system (with option to abort).

share|improve this answer
Great outside-the-box answer! We'll give it a shot. Thank you. – Roger Jun 21 '12 at 16:23
That did the trick. We added an extra condition to exit if it isn't Saturday or Sunday, since we only want this to happen over the weekend, but this was what we needed. Thanks again. – Roger Jun 22 '12 at 20:35

Wscript.Quit with no argument returns errorlevel "0", which means "no error".

So, specify a .Quit argument to cause an exit with an errorlevel other than "0" when the user choses to abort:

If intShutdown = vbYes Then
  ' Abort Shutdown
  strAbort = "shutdown.exe -a"
  set objShell = CreateObject("WScript.Shell")
  objShell.Run strAbort, 0, false
  ' Abort Not Requested
End if


So, as @Indrek points out, it turns out this won't work. As the (2008) Task Scheduler reports the result code in the UI, but actually only considers if the task ran or not, regardless of script result codes.

The Task Scheduler cannot read the result and the return code of the script. It can only get the task status which is that whether the task has run or not. If the task has run, whatever the result of the task, Task Scheduler will consider that the task is successful.


share|improve this answer
Won't work, Task Scheduler doesn't look at process exit codes. – Indrek Jun 21 '12 at 15:35
Well look at that, you're right. It reports the return code, but doesn't consider the task a failure just because the script failed. Because the task still completed. That's stupid! I'll update my answer. – Ƭᴇcʜιᴇ007 Jun 21 '12 at 15:42
That's where I had gotten in my research. Appreciate the thought, though. – Roger Jun 21 '12 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .