How can I cause Task Scheduler to "fail" if a dialog box returns a certain result?

Posted by Roger on Super User See other posts from Super User or by Roger
Published on 2012-06-21T15:01:22Z Indexed on 2012/06/21 15:18 UTC
Read the original article Hit count: 323

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

Wscript.Quit

Appreciate any thoughts.

© Super User or respective owner

Related posts about task-scheduler

Related posts about vbscript