Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

Why my while loop not working?

Correction!!!  I do not see this program running in the Task Manager.  

This vb script supposed to reboots the pc .  It pops up a dialog that has a Yes and a No buttons that does the following.  
 
If they click Yes then restart, if they click No, delay for 10 minutes, then prompt again.  
 
If they hit No a second time, delay 10 minutes, and then prompt again.
 
If they hit No a third time, delay 10 minutes and then prompt again.
 
On the 4th time, give them with a message that the computer will restart in 5 minutes with a count down, and then restart the box.

I run the script and it promopts me, I selected no.   I never got the popup dialog box again.  Please help on how to fix this.
pcReboot2.vbs
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Try this:
'Give users 60 seconds to respond
Const TIMEOUT = 60
Dim numOfDelays
numOfDelays = 0

Call Main

Sub RebootPc()
    strComputer = "."
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'explorer.exe'")
    For Each objProcess In colProcessList
        objProcess.Terminate
    Next
    Set OpSysSet = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
    For Each OpSys In OpSysSet
        OpSys.Reboot
    Next
End Sub

Sub DelayOrReboot()
Set objShell = WScript.CreateObject("WScript.Shell")
    If (numOfDelays < 3) Then
        WScript.Sleep (1000 * 60 * 10)
        numOfDelays = numOfDelays + 1
        Call Main
    Else
        Call RebootPc
    End If
End Sub

Sub Main()
    Do While iRetVal < 3
        iRetVal = objShell.Popup("Your computer will restart momentarily.  To restart immediately, click Yes. You can delay the reboot by 10 minutes each time you click the No button with a maximum of 3 delays.", TIMEOUT, "IT Maintenance", vbExclamation + vbYesNo + vbDefaultButton3)

        Select Case iRetVal
            Case vbYes
                Call RebootPc
            Case vbNo
                Call DelayOrReboot
            Case Else
                Call DelayOrReboot
        End Select
    Loop
End Sub

Open in new window

Avatar of lapucca
lapucca

ASKER

got error, please see attached.  
I think it needs "objShell = WScript.CreateObject("WScript.Shell")"
Quesiton, do I need to create a new object shell each time  I need to run a objShell codes?  In C# I usually dispose object to clean up.  Do I not need to do this in VBScript?
Thank you.
codeErr.jpg
I see several issues, and will take a look later tonight if this is still open...

~bp
ASKER CERTIFIED SOLUTION
Avatar of Joe Howard
Joe Howard
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lapucca

ASKER

Thank you both for responding.  I found my own logic error.  I used the wrong variable here
Sub Main()
    If numOfDelays < 3 Then
(I used iRetVal before and that's wrong)