如何使用 VBScript 强制重启 Windows 机器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/188636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to force restart a Windows box using VBScript?
提问by tloach
I'm trying to find a way to force Windows to reboot, and I am running into issues. I've tried
我试图找到一种方法来强制 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
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?
我也尝试过使用该shutdown -f -r
命令,但在这两种情况下,有时我都没有收到任何响应,如果我再试一次,即使我离开它多长时间,我也会收到一条错误消息,提示“操作无法完成,因为系统正在关闭”它没有关闭,它仍然允许我启动新程序,并且执行一个shutdown -a
给出相同的错误。如何使用脚本强制 Windows 重新启动?
回答by Matt Hanson
Try replacing:
尝试更换:
retVal = OpSys.Reboot()
With:
和:
retVal = OpSys.Win32Shutdown(6)
回答by Mike L
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.
嗯,这使用了 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
What OS are you running against? This test was against XP. I wonder if the server OS requires a shutdown code...
你在运行什么操作系统?这个测试是针对 XP 的。我想知道服务器操作系统是否需要关闭代码...
回答by Jason
You can also try the psShutdown command line utility from Sysinternals now Microsoft. http://technet.microsoft.com/en-us/sysinternals/bb897541.aspx
您还可以尝试 Sysinternals 现在 Microsoft 的 psShutdown 命令行实用程序。 http://technet.microsoft.com/en-us/sysinternals/bb897541.aspx
回答by Ossama
'*********************************************************
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.
这个小脚本会在 0 秒后重新启动本地计算机。
回答by Hakan ?EL?K
Set Reset= WScript.CreateObject ("WScript.Shell")
Reset.run "shutdown -r -t 00", 0, True
Or..
或者..
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