Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 991
tloach Avatar asked Oct 09 '08 18:10

tloach


People also ask

How to restart a computer using vbs script?

set objShell = wscript. CreateObject("wscript. shell") objShell. Run "shutdown.exe /R /T 5 /C ""Rebooting your computer now!

How do you restart Windows box?

Type msconfig in the search box, and select System Configuration from the list of results. Select the Services tab, and then select Hide all Microsoft services. Select each of the check boxes in the upper half of the Service list. Select OK, then select Restart.


1 Answers

Try replacing:

retVal = OpSys.Reboot()

With:

retVal = OpSys.Win32Shutdown(6)
like image 157
Matt Hanson Avatar answered Sep 24 '22 04:09

Matt Hanson