Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Why my while loop not working?

Posted on 2014-04-03
5
Medium Priority
?
244 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 57

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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

705 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