2014-10-08 125 views
1

我需要一種更簡潔/更有效的方式來通過VBS重新啓動計算機。下面的代碼是我到目前爲止的一個例子。有沒有更有效的方法來重新啓動其他SendKeys方法?通過VBS重新啓動計算機

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 

感謝

+5

爲什麼'SendKeys'?只需運行可執行文件。 'CreateObject(「WScript.Shell」)。運行「shutdown.exe/r」' – Bond 2014-10-08 23:42:29

回答

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 
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 
相關問題