'==========================================================================
'
' Name: NiceReboot.vbs
' Author: Steve Pruitt
' Date: 10/31/2006
'
' 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
' Nicereboot.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
' 1st parameter is nag interval in minutes
If objArgs.count > 0 Then
iInterval = objArgs(0)
Else
iInterval = 10 ' 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 ""All ConfigMgr client components 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("ConfigMgr client components have been installed to your computer. In order for the ConfigMgr client install to complete, your computer must be restarted." & vbCRLF & vbCRLF & "If all of your applications and data are closed, is it ok to restart your computer now?", 600, "ConfigMgr client component install", 4 + 32)
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, "ConfigMgr client component install", 4 + 32)
if iResponse = 6 then
' REBOOT PC
'iReturn = WshShell.Run("shutdown.exe -r -t 15 -c ""All ConfigMgr client components 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
From: admin@lists.myITforum.com [mailto:admin@lists.myITforum.com] On Behalf Of Joseph Crawford
Sent: Monday, January 05, 2009 5:34 PM
To: mssms@lists.myitforum.com
Subject: RE: [mssms] GracefulReboot.vbs ??
My pleasure
From: admin@lists.myITforum.com [mailto:admin@lists.myITforum.com] On Behalf Of Troy Martin
Sent: Monday, January 05, 2009 5:29 PM
To: mssms@lists.myITforum.com
Subject: [mssms] GracefulReboot.vbs ??
A while back, I attached this script to an email I posted. I’ve since lost my archived emails, so I no longer have it.
Can someone search their archives for this file and send it to me? You can just search for GracefulReboot and forward the latest one.
Thanks
Troy L. Martin | Senior Consultant | 1E |
Mobile: 678 898 6147 | US/Canada Toll Free: 1 866 592 4214
troy.martin@1e.com | www.1e.com
DISCLAIMER: This is a PRIVATE AND CONFIDENTIAL message for the ordinary user of this email address. If you are not the intended recipient, please delete without copying and kindly advise us by e-mail of the mistake in delivery. NOTE: Regardless of content, this e-mail shall not operate to bind 1E Ltd to any order or other contract unless pursuant to explicit written agreement or government initiative expressly permitting the use of e-mail for such purpose.
==============
Missed an email? Check out the list archive:
http://myitforum.com/cs2/blogs/smslist/
==============
Missed an email? Check out the list archive:
http://myitforum.com/cs2/blogs/smslist/