如何使用VBScript强制重启Windows电脑?

8
我正在尝试寻找一种强制重启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 重新启动?

5个回答

10

尝试替换:

retVal = OpSys.Reboot()

使用:

retVal = OpSys.Win32Shutdown(6)

我猜你的意思是 Win32ShutDown 吗? - tloach
1
我同意Matt的看法,这是正确的方法。MSDN详细信息:http://msdn.microsoft.com/zh-cn/library/aa394058(VS.85).aspx - unrealtrip

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

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 0", 0, True

或者..

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接