How to force restart a Windows box using VBScript?
Asked Answered
F

5

8

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?

Forayer answered 9/10, 2008 at 18:43 Comment(0)
B
10

Try replacing:

retVal = OpSys.Reboot()

With:

retVal = OpSys.Win32Shutdown(6)
Baese answered 9/10, 2008 at 19:32 Comment(2)
I assume you mean Win32ShutDown?Forayer
I'm with Matt, this is the way to go. MSDN details: msdn.microsoft.com/en-us/library/aa394058(VS.85).aspxCoronagraph
P
4

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...

Portis answered 9/10, 2008 at 19:23 Comment(0)
M
3

You can also try the psShutdown command line utility from Sysinternals now Microsoft. http://technet.microsoft.com/en-us/sysinternals/bb897541.aspx

Maurita answered 10/10, 2008 at 3:45 Comment(0)
S
2
'*********************************************************

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.

Samaniego answered 8/6, 2011 at 2:29 Comment(0)
K
0
Set Reset= WScript.CreateObject ("WScript.Shell")

Reset.run "shutdown -r -t 0", 0, True

Or..

Shell "shutdown -r -f -t 0"   ' for restart

Shell "shutdown -s -f -t 0"  ' for Shutdown

Shell "shutdown -l -f -t 0"   ' for log off

Shell "shutdown -a "  ' for abort
Kaffiyeh answered 11/4, 2019 at 10:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.