tloach
tloach

Reputation: 8040

How to force restart a Windows box using VBScript?

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?

Upvotes: 8

Views: 52090

Answers (5)

Hakan ÇELİK
Hakan ÇELİK

Reputation: 39

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

Upvotes: 0

Ossama
Ossama

Reputation: 21

'*********************************************************

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.

Upvotes: 2

Jason
Jason

Reputation: 243

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

Upvotes: 3

Matt Hanson
Matt Hanson

Reputation: 3504

Try replacing:

retVal = OpSys.Reboot()

With:

retVal = OpSys.Win32Shutdown(6)

Upvotes: 10

Mike L
Mike L

Reputation: 4913

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

Upvotes: 4

Related Questions