How to force restart a Windows box using VBScript?
Question
I'm trying to find a way to force Windows to reboot, and I am running into issues. I've tried
Set OpSysSet = GetObject("winmgmts:{authenticationlevel=Pkt," _
& "(Shutdown)}").ExecQuery("select * from Win32_OperatingSystem where "_
& "Primary=true")
for each OpSys in OpSysSet
retVal = OpSys.Reboot()
next
I've also tried using the shutdown -f -r
command, and in both cases I sometimes get no response, and if I try again I get an error saying "Action could not complete because the system is shutting down" even though no matter how long I leave it it doesn't shut down, it still allows me to start new programs, and doing a shutdown -a
gives the same error. How can a script be used to force Windows to reboot?
Solution
Try replacing:
retVal = OpSys.Reboot()
With:
retVal = OpSys.Win32Shutdown(6)
OTHER TIPS
Well, this uses VBScript -- although truthfully it invokes the same command line shutdown that you're trying to do. I've tested it and it works.
Dim oShell
Set oShell = CreateObject("WScript.Shell")
'restart, wait 5 seconds, force running apps to close
oShell.Run "%comspec% /c shutdown /r /t 5 /f", , TRUE
What OS are you running against? This test was against XP. I wonder if the server OS requires a shutdown code...
You can also try the psShutdown command line utility from Sysinternals now Microsoft. http://technet.microsoft.com/en-us/sysinternals/bb897541.aspx
'*********************************************************
Option Explicit
Dim objShell
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"
'*********************************************************
This little script restarts the local computer after 0 seconds.
Set Reset= WScript.CreateObject ("WScript.Shell")
Reset.run "shutdown -r -t 00", 0, True
Or..
Shell "shutdown -r -t 00" ' for restart
Shell "shutdown -s -t 00" ' for Shutdown
Shell "shutdown -l -t 00" ' for log off
Shell "shutdown -a -t 00" ' for abort