Solved

Why my while loop not working?

Posted on 2014-04-03
5
230 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
  • 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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now