Vbscript 如何在自动重新启动时超时此消息框?
这将使用Vbscript 如何在自动重新启动时超时此消息框?,vbscript,timeout,message,reboot,Vbscript,Timeout,Message,Reboot,这将使用WScript.Shell.Popup函数()替换MsgBox。如果选择了Yes,或未按下任何按钮,则循环结束并达到停机代码 option explicit on error resume next Dim strComputer, intRebootChoice Dim objWMIService, objOperatingSystem Dim colOperatingSystems strComputer = "." do while 1>0 in
WScript.Shell.Popup
函数()替换MsgBox。如果选择了Yes
,或未按下任何按钮,则循环结束并达到停机代码
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
这将使用
WScript.Shell.Popup
函数()替换MsgBox。如果选择了Yes
,或未按下任何按钮,则循环结束并达到停机代码
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
如前所述:使用而不是MsgBox
。我还建议对代码进行一些其他修改:
- 在没有适当的错误处理例程的情况下,不要在任何地方使用“错误恢复下一步”,也不要在全局范围内使用它
- 您仅在本地计算机上使用WMI,因此没有必要通过
配置计算机strComputer
- 尽可能使用命名常量而不是文字
- 在“独立”函数/方法调用中,不要在参数列表周围使用括号。它只适用于单个参数,当您有多个参数时,它将中断。请参阅以获取解释
比Do While True
更容易理解Do While 1>0
- 一个空的
是没有意义的。移除它Case Else
时间较长,不太准确。更准确的方法是短时间睡眠,直到达到预定义的时间点:睡眠
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 ....
- 是毫无意义的,特别是在弱类型语言(如VBScript)中
endtime = DateAdd("h", 4, Now)
Do Until Now >= endtime
WScript.Sleep 500
Loop
请注意,在当前形式中,“否”是默认选择,因此如果没有用户交互,系统将永远不会重新启动。如果要更改默认设置,以便在用户不执行任何操作时重新启动系统,请将timeUp
移至另一种情况:
默认“否”:
默认“是”:
同时从弹出窗口中删除+vbDefaultButton2
,使Enter选择“是”而不是“否”。如前所述:使用而不是MsgBox
。我还建议对代码进行一些其他修改:
- 在没有适当的错误处理例程的情况下,不要在任何地方使用“错误恢复下一步”
,也不要在全局范围内使用它
strComputer
配置计算机Do While True
比Do While 1>0
更容易理解Case Else
是没有意义的。移除它睡眠
时间较长,不太准确。更准确的方法是短时间睡眠,直到达到预定义的时间点:
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
....
endtime = DateAdd("h", 4, Now)
Do Until Now >= endtime
WScript.Sleep 500
Loop
请注意,在当前形式中,“否”是默认选择,因此如果没有用户交互,系统将永远不会重新启动。如果要更改默认设置,以便在用户不执行任何操作时重新启动系统,请将timeUp
移至另一种情况:
默认“否”:
默认“是”:
同时从
弹出窗口语句中删除+vbDefaultButton2
,使Enter选择“是”而不是“否”。是否希望消息框在特定时间后消失?是否希望消息框在特定时间后消失?+1,很好的建议。但最后一段是错误的。在没有用户交互的情况下,弹出方法返回-1,而不是与默认按钮关联的值(在本例中为7)。vbDefaultButton…
选项只需在界面中选择默认按钮,如果用户直接按Enter
+1,该按钮将是选中的控件,这是很好的建议。但最后一段是错误的。在没有用户交互的情况下,弹出方法返回-1,而不是与默认按钮关联的值(在本例中为7)。vbDefaultButton…
选项只需在界面中选择默认按钮,如果用户直接按Enter