Solved

Why my while loop not working?

Posted on 2014-04-03
5
227 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 26

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 51

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 26

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

16 Experts available now in Live!

Get 1:1 Help Now