如何使用VBScript强制重启Windows框?

问题描述 投票:8回答:5

我正试图找到一种方法来强制Windows重新启动,我遇到了问题。我试过了

Set OpSysSet = GetObject("winmgmts:{authenticationlevel=Pkt," _
     & "(Shutdown)}").ExecQuery("select * from Win32_OperatingSystem where "_
     & "Primary=true")
for each OpSys in OpSysSet
    retVal = OpSys.Reboot()
next

我也试过使用shutdown -f -r命令,在这两种情况下我有时都没有得到任何响应,如果我再试一次,我会收到一条错误,说“行动无法完成,因为系统正在关闭”,即使我不管多久都离开它没有关闭,它仍然允许我启动新程序,并做一个shutdown -a给出相同的错误。如何使用脚本强制Windows重新启动?

windows vbscript reboot
5个回答
9
投票

尝试更换:

retVal = OpSys.Reboot()

附:

retVal = OpSys.Win32Shutdown(6)

4
投票

好吧,这使用VBScript - 虽然它实际上调用了你想要做的相同的命令行关闭。我测试了它,它的工作原理。

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

您正在运行什么操作系统?这个测试是针对XP的。我想知道服务器操作系统是否需要关机代码......


3
投票

您也可以从Sysinternals现在的Microsoft尝试psShutdown命令行实用程序。 http://technet.microsoft.com/en-us/sysinternals/bb897541.aspx


2
投票
'*********************************************************

Option Explicit

Dim objShell

Set objShell = WScript.CreateObject("WScript.Shell")

objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 0"

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

这个小脚本在0秒后重新启动本地计算机。


0
投票
Set Reset= WScript.CreateObject ("WScript.Shell")

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

要么..

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
© www.soinside.com 2019 - 2024. All rights reserved.