Link to home
Start Free TrialLog in
Avatar of 1manshow
1manshow

asked on

Problem with VB Script Value

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
ASKER CERTIFIED SOLUTION
Avatar of rlandquist
rlandquist
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 1manshow
1manshow

ASKER

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
You have the iInterval variable = 180 * 60 * 1000. = 180 minutes.   When I change this is seems to work.
Correct.
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.
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

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

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tried it and nothing pops back up...Weird
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!
Avatar of Guy Hengel [angelIII / a3]
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.