Solved

Why my while loop not working?

Posted on 2014-04-03
5
237 Views
Last Modified: 2014-04-03
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
0
Comment
Question by:lapucca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39976485
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

0
 

Author Comment

by:lapucca
ID: 39976524
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
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39976577
I see several issues, and will take a look later tonight if this is still open...

~bp
0
 
LVL 27

Accepted Solution

by:
MacroShadow earned 500 total points
ID: 39976595
Here this works (I think):
'Give users 60 seconds to respond
Const TIMEOUT = 60
Dim numOfDelays
numOfDelays = 0
Set objShell = WScript.CreateObject("WScript.Shell")

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()
    If (numOfDelays < 3) Then
        WScript.Sleep (1000 * 60 * 10)
        numOfDelays = numOfDelays + 1
        Call Main
    Else
        Call RebootPc
    End If
End Sub

Sub Main()
    If iRetVal < 3 Then
        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
    Else
        Call RebootPc
    End If
End Sub
                                            

Open in new window

0
 

Author Closing Comment

by:lapucca
ID: 39976715
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)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question