如何通过自动重启超时此消息框?

时间:2014-07-29 17:52:15

标签: vbscript timeout message reboot

option explicit  
on error resume next


Dim strComputer, intRebootChoice  
Dim objWMIService, objOperatingSystem  
Dim colOperatingSystems 

strComputer = "."


do while 1>0  
 intRebootChoice = msgbox("A system update requires a reboot. Please reboot now by clicking Yes.  Choose No to be asked again in 4 hours.",308,"NOTICE - REBOOT REQUIRED")

select case intRebootChoice  

  case 6  
   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(1)  
   Next 

  case 7  
   wscript.sleep 14400000 

  case else

end select

loop

2 个答案:

答案 0 :(得分:2)

这使用WScript.Shell.Popup函数(documentation)来替换MsgBox。如果选择了Yes或没有按下任何按钮,则循环结束并到达关闭代码。

Const NO = 7

    Do While NO = WScript.CreateObject("WScript.Shell").Popup( _ 
            "A system update requires a reboot. Please reboot now by clicking Yes.  Choose No to be asked again in 4 hours.", _ 
            10, _ 
            "NOTICE - REBOOT REQUIRED", _ 
            308 _ 
        )
        WScript.Sleep 14400000 
    Loop

    ' Reboot code
    ....

答案 1 :(得分:2)

正如MC ND已经指出:使用Popup代替MsgBox。我还建议您对代码进行一些其他修改:

  • 在没有适当的错误处理例程的情况下,不要在任何地方使用On Error Resume Next ,并且从不全局使用它。
  • 您只在本地计算机上使用WMI,因此无法通过strComputer配置计算机。
  • 尽可能使用命名常量而不是文字。
  • 不要在#34; standalone"中的参数列表周围使用括号。函数/方法调用。它只适用于单个参数的巧合,并且当你有多个参数时会中断。有关说明,请参阅here
  • Do While TrueDo While 1>0更容易理解。
  • Case Else毫无意义。删除它。
  • Sleep在较长时间内不是很准确。更准确的方法是短时间睡眠,直到达到预定义的时间点:

    endtime = DateAdd("h", 4, Now)
    Do Until Now >= endtime
      WScript.Sleep 500
    Loop
    
  • Hungarian notation毫无意义,特别是在像VBScript这样的弱类型语言中。

修改后的代码:

Option Explicit

Const timeout   = 10 'seconds
Const sleeptime = 4  'hours
Const timeUp    = -1

Dim sh, rebootChoice, wmi, os, endtime

Set sh = CreateObject("WScript.Shell")

Do While True
  rebootChoice = sh.Popup("A system update requires a reboot. " & _
                          "Please reboot now by clicking Yes. " & _
                          "Choose No to be asked again in 4 hours.", timeout, _
                          "NOTICE - REBOOT REQUIRED", _
                          vbYesNo + vbExclamation + vbDefaultButton2)

  Select Case rebootChoice
    Case vbYes
      Set wmi = GetObject("winmgmts:" & _
                          "{impersonationLevel=impersonate,(Shutdown)}!" & _
                          "\\.\root\cimv2")
      For Each os In wmi.ExecQuery("SELECT * FROM Win32_OperatingSystem")
        os.Reboot 1
      Next
    Case vbNo, timeUp
      endtime = DateAdd("h", sleeptime, Now)
      Do Until Now >= endtime
        WScript.Sleep 500
      Loop
  End Select
Loop

请注意,在当前表格中"否"是默认选择,因此无需用户交互,系统将永远不会重新启动。如果要更改默认值以在用户什么都不做的情况下重新启动系统,请将timeUp移到另一个案例:

默认"否":

Case vbYes
  ...
Case vbNo, timeUp
  ...

默认"是":

Case vbYes, timeUp
  ...
Case vbNo
  ...

同时从+ vbDefaultButton2语句中删除Popup以使输入选择"是"而不是" No"。