Tuesday, May 17, 2011

Windows Shutdown VBScript

A friend of mine asked me to help figure out why her shutdown script was not working. For those who don't care to read that whole article, it looked something like this:

@echo off
echo Scheduling shutdown in 1.5 hours.
shutdown.exe /h /t 5400

As it turns out, you can't actually use /h with /t. (Although /s works fine with /t) My solution was to learn some VBScript and kill the problem with excessive firepower! All you need to do is type the following code into an editor and save it as a *.vbs file. (I called mine Hibernate.vbs) I placed it right on the desktop for quick access.

hours = 0.001 'Specify the # of hours to sleep here
seconds = hours * 60 * 60
ms = seconds * 1000

Set wshShell = WScript.CreateObject("WScript.Shell")
WScript.Echo("Hibernate in " & seconds & " seconds")
WScript.Sleep(ms)
cancel = wshShell.Popup("Cancel Hibernate?", 10, "System Shutdown!", VbYesNo + VbExclamation)
If cancel = VbYes Then
  WScript.Quit(0)
End If
wshShell.Run("shutdown.exe /h")

This script will inform the user that hibernation will occur. Once the user presses OK then it will wait the specified number of hours before re-informing the user that hibernation is happening. If the user doesn't respond or opt to cancel, then the script continues and the computer is sent into hibernation.

No comments:

Post a Comment