Solved

Problem with VB Script Value

Posted on 2010-08-12
13
322 Views
Last Modified: 2012-08-14
I have a script to notify a user to reboot after the install of Windows Updates.
If the user selects no to the reboot request then the script should pop back up and ask again every 5 minutes. However, the script does not seem to pop back up to ask again. Other than that the script works fine.

Can someone please take a look and see what is missing or misconfigured in this script to have it ask every 5 minutes. -Thanks


' This script checks if a reboot is required, and exits if not
' Then it checks if a user is logged on. If not, it reboots
' Otherwise it nags the user to reboot

' Nagreboot.vbs [nag interval] [nag retries]
' if 2nd param is used, first one must also be supplied
'
'==========================================================================
On Error Resume Next

'Define Variables
Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager"
bRoot = False
bSys32 = False
bWBEM = False
bRebootNeeded = False

Dim WshShell, iNagCount, iVar
Set WshShell = WScript.CreateObject("WScript.Shell")
'=======================
'Process Arguments
Set objArgs = WScript.Arguments

WScript.Timeout = 30

' 1st parameter is nag interval in minutes
If objArgs.count > 0 Then
      iInterval = objArgs(0)
Else
      iInterval = 180      ' default is nag every 10 minutes
End If
iInterval = iInterval*60*1000      'convert to milliseconds

' 2nd parameter is # nag retries
If objArgs.count > 1 Then
      iNagCount = objArgs(1)
Else
      iNagCount = 99999      ' default is nag forever
End If



'=======================
'Connect to WMI Registry
'=======================
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")

If Err.Number <> 0 Then
      WScript.Quit Err.Number
End If

'=======================
'Get Path
'=======================
Return = objReg.GetMultiStringValue(HKEY_LOCAL_MACHINE,_
    strKeyPath,"PendingFileRenameOperations", strValue)
If (Return = 0) And (Err.Number = 0) Then
      bRebootNeeded = True
End If

'==================
' Determine if any user is logged on
'==================
bLoggedOn = False
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")

If Err.Number <> 0 Then
      WScript.Quit Err.Number
End If

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
iUsers = 0
For Each objItem In colItems
      If objItem.UserName > "" Then      
            bLoggedOn = True
      End If
Next

' ======================
' Exit if no reboot needed
' ======================

If bRebootNeeded = False Then      ' exit, no action needed
      Wscript.Quit 0
End If


' ======================
' Reboot if needed and no user logged on
' ======================
If bLoggedOn = False Then    
      'iReturn = WshShell.Run("shutdown.exe -r -t 15 -c ""Software update will be successfully installed after the restart.""", 0, false)
      RebootComputer 15,6
      Wscript.Quit 0
End If

 ' ======================
 ' Reboot is needed, but user is logged on
 ' Nag them until they reboot
 ' ======================
 
'----------------------------------------------------------------
' Warn the user iNagCount times, every iInterval milliseconds, giving option to restart.
'----------------------------------------------------------------


For iVar = 1 To iNagCount
    iResponse = WshShell.Popup("IT recently updated this computer with the most current security patches from Microsoft. To ensure your system is fully protected you must reboot your workstation. Please save any data and close all applications." & vbCRLF & vbCRLF & "If you have questions please contact the HelpDesk at x1234." & vbCRLF & vbCRLF & "Proceed with reboot now?", 0, "Software Installation", 4 + 32 + 4096)
    Select Case iResponse
        Case 7, -1 ' NO clicked or timeout
            Wscript.Sleep iInterval      ' Sleep for one period
        Case 6 ' YES clicked
            Wscript.Sleep 1000      ' Wait one second
            ' Double-check that user wants to reboot
            'iResponse = WshShell.Popup("Are your sure it's ok restart your computer now?", 0, "Software Installation", 4 + 32)
            if iResponse = 6 then
                ' REBOOT PC
                'iReturn = WshShell.Run("shutdown.exe -r -t 15 -c ""Software update will be successfully installed after the restart.""", 0, false)
                RebootComputer 15,6
                WScript.Quit (0)
            Else
                        Wscript.Sleep iInterval      ' Sleep for one period
            End if
        Case else ' Unknown iResponse
            Wscript.Sleep iInterval      ' Sleep for one period
    End Select
Next


WScript.Quit (0)


Sub RebootComputer(ByVal intTimeOut, ByVal intFlags)

      'Set objArgs = Wscript.Arguments

      'intTimeOut = objArgs(0) 'Countdown in seconds
      'intFlags = objArgs(1) 'Bitmapped set of flags to shutdown the computer: 0 = Logoff, 4 = Forced Logoff (0+4),
                                      '1 = Shutdown, 2 = Reboot, 6 = Forced Reboot (2+4), 8 = Power Off, 12 = Forced Power Off (8+4)

      intTimeOutSec = intTimeOut * 1000 'Convert from milliseconds to seconds

      strComputer = "."

      Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
                  strComputer & "\root\cimv2")

      Set colOperatingSystems = objWMIService.ExecQuery _
            ("Select * from Win32_OperatingSystem")

      For Each objOperatingSystem in colOperatingSystems
            Wscript.Sleep intTimeOutSec 'Pause for x seconds before rebooting computer
            objOperatingSystem.Win32Shutdown intFlags
      Next

End Sub
0
Comment
Question by:1manshow
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 12

Accepted Solution

by:
rlandquist earned 250 total points
ID: 33422899
If objArgs.count > 0 Then
      iInterval = objArgs(0)
Else
      iInterval = 180      ' default is nag every 10 minutes
End If
iInterval = iInterval*60*1000      'convert to milliseconds

According to this part of your script, you set iInterval to 180 and then multiply it by 60 and then 1000,
so you are telling it to sleep for 180 MINUTES or 3 HOURS!

You need to change iInterval = 180 to iInterval = 5 or remove the * 60 from the calculation!

I hope that makes sense.
0
 

Author Comment

by:1manshow
ID: 33423040
Like this??

Dim WshShell, iNagCount, iVar
Set WshShell = WScript.CreateObject("WScript.Shell")
'=======================
'Process Arguments
Set objArgs = WScript.Arguments

WScript.Timeout = 30

' 1st parameter is nag interval in minutes
If objArgs.count > 0 Then
      iInterval = objArgs(0)
Else
      iInterval = 5      ' default is nag every 10 minutes
End If
iInterval = iInterval*60000      'convert to milliseconds
0
 
LVL 1

Expert Comment

by:RobGar05
ID: 33423105
You have the iInterval variable = 180 * 60 * 1000. = 180 minutes.   When I change this is seems to work.
0
 
LVL 12

Expert Comment

by:rlandquist
ID: 33423110
Correct.
0
 

Author Comment

by:1manshow
ID: 33423184
Sorry for the confusion but when I convert 5 minutes into millisecond it comes out to 300000 -converted. If I change it to 1000 as a test it seem to pop up but very fast like a second - hence the conversion. So my questions is the I have changed the interval = 5 so do I need the line

iInterval = iInterval*60000      'convert to milliseconds

or should I remove it.

Uour orignal answer was to remove *60 and change the 180 to "5". Would it be possible to paste the script back in the way it should be to ensure I'm getting this right. -Sorry but I wasn't sure and now it not popping up anymore.
0
 
LVL 12

Expert Comment

by:rlandquist
ID: 33423407
To convert Minutes to Milliseconds you use:
minutes * 60,000
so 1 minute = 60,000 milliseconds
5 minutes =  300,000 milliseconds


' 1st parameter is nag interval in minutes

If objArgs.count > 0 Then

      iInterval = objArgs(0)

Else

      iInterval = 5      ' default is nag every 5 minutes

End If

iInterval = iInterval * 60000      'convert to milliseconds

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:1manshow
ID: 33423924
when I use iInterval = iInterval * 1000 it wor ks fine and pops up fast but when I try to change to 60000 or 300000 it doesn't seem to pop up. Not sure if the open browser is affecting it but I have IE open as a testto see if users had an open window then this should pop up over the top and display. You mentioned that you tested it and it worked. Did you have anything open?  Here is what I have it at right now. Am I missing something here. Thanks for everyone's help and sorry for the back and fourth. This doesn't seem to hard but once I change the value from 1000 to what ever it stops working.

On Error Resume Next

'Define Variables
Const HKEY_LOCAL_MACHINE = &H80000002
strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager"
bRoot = False
bSys32 = False
bWBEM = False
bRebootNeeded = False

Dim WshShell, iNagCount, iVar
Set WshShell = WScript.CreateObject("WScript.Shell")
'=======================
'Process Arguments
Set objArgs = WScript.Arguments

WScript.Timeout = 30

' 1st parameter is nag interval in minutes
If objArgs.count > 0 Then
      iInterval = objArgs(0)
Else
      iInterval = 5      ' default is nag every 10 minutes
End If
iInterval = iInterval *60000      'convert to milliseconds

' 2nd parameter is # nag retries
If objArgs.count > 1 Then
      iNagCount = objArgs(1)
Else
      iNagCount = 99999      ' default is nag forever
End If

0
 
LVL 12

Expert Comment

by:rlandquist
ID: 33424323
You don't change the big number.  You only change the number of minutes on this line:
iInterval = 5      ' default is nag every 10 minutes
Leave this line alone:
iInterval = iInterval * 60000      'convert to milliseconds

Change iInterval = 5 to iInterval = 1 to test one minute, see if that works then change back to 5.
0
 
LVL 1

Assisted Solution

by:RobGar05
RobGar05 earned 250 total points
ID: 33424430
It looks like the problem is with time wscript.timeout object.  Just comment it out or set it to something higher.  If you open task manager and watch the wscript.exe process you can see it ending after a little while.
0
 

Author Comment

by:1manshow
ID: 33424510
tried it and nothing pops back up...Weird
0
 

Author Comment

by:1manshow
ID: 33424612
Thaks Everyone! It works now after commenting out the timeout line.

It looks like the problem is with time wscript.timeout object.  Just comment it out or set it to something higher.  If you open task manager and watch the wscript.exe process you can see it ending after a little while.

You guys rock!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34977948
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.
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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
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.

762 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

19 Experts available now in Live!

Get 1:1 Help Now