Reputation: 1
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
Upvotes: 0
Views: 1424
Reputation: 200293
As MC ND already pointed out: use Popup
instead of MsgBox
. I'd also recommend some other modifications to your code:
On Error Resume Next
anywhere without proper error handling routines in place, and NEVER use it globally.strComputer
.Do While True
is easier to understand than Do While 1>0
.Case Else
is pointless. Remove it.Sleep
is not very accurate with longer periods of time. A more accurate method is sleeping for short periods until a predefined point in time is reached:
endtime = DateAdd("h", 4, Now)
Do Until Now >= endtime
WScript.Sleep 500
Loop
Modified code:
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
Note that in the current form "No" is the default choice, so without user interaction the system will never reboot. If you want to change the default to make the system reboot when the user does nothing, move timeUp
to the other case:
Default "No":
Case vbYes
...
Case vbNo, timeUp
...
Default "Yes":
Case vbYes, timeUp
...
Case vbNo
...
Also remove + vbDefaultButton2
from the Popup
statement to make Enter select "Yes" instead of "No".
Upvotes: 2
Reputation: 70923
This uses the WScript.Shell.Popup
function (documentation) to replace the MsgBox. If Yes
is selected or no button is pressed, the loop ends and the shutdown code is reached.
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
....
Upvotes: 2