通过VBS重新启动计算机

时间:2014-10-08 22:52:15

标签: vbscript

我需要一种不那么痛苦/更有效的方式来通过VBS重启我的电脑。下面的代码是我到目前为止的一个例子。是否有更有效的方法重新启动其他SendKeys方法??

Option Explicit
Dim obj
set obj= creatobject("wscript.shell")    
    obj.run "CMD"
    wscript.sleep 300
    obj.SendKeys "shutdown /r"
    obj.SendKeys "{ENTER}"
    obj.SendKeys "exit"
    obj.SendKeys "{ENTER}"
    wscript.quit

由于

2 个答案:

答案 0 :(得分:2)

您可以使用winmgmts,此脚本会为计算机名称提供参数,但可以轻松更改以供您使用

If Wscript.Arguments.Count = 0 Then
    strComputer = inputbox("Enter a computer name to Restart","Enter computer name")
    if strComputer = "" then wscript.quit
Else
    strCOmputer = Wscript.Arguments.Item(0)

End If

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
        strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
    objOperatingSystem.Reboot()
Next

答案 1 :(得分:0)

它会起作用

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