Restart PC if pending reboot

This script checks if there is any rename operations pending or updates pending and initiates a restart of the computer if required.
By running this on demand you can ensure that computers are rebooted only if required,minimizing the effects on users and maximising up time.

Dim strComputer
Dim strKeyPath
Dim strValueName
Dim strValue
Dim arrValues
Dim bReboot
Dim oReg
Dim objSysInfo
dim objShell

On Error Resume Next
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_MULTI_SZ = 7
strComputer = "."
bReboot = False

Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager"
strValueName = "PendingFileRenameOperations"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,arrValues

If Not IsNull(arrValues) Then
  For Each strValue In arrValues
'Pending Rename operations found, reboot
      bReboot = True
      Exit For
  Next
Else
	'No Pending Rename operations
End If

'Only Check for update reboots if we need to
If bReboot = False Then
	Set objSysInfo = CreateObject("Microsoft.Update.SystemInfo")
	bReboot = objSysInfo.RebootRequired
End If

If bReboot = True then
	strCmd = "shutdown -r -t 120 -f /c " & chr(34) & "A system reboot has been initiated, please save all your work." & chr(34)
	
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run strCmd, 0, false 
End If

objSysInfo = Nothing
objShell = Nothing

Leave a Reply

Your email address will not be published.