From time to time in Windows administration and patch management, it's necessary to know whether a machine you're about to do something to is waiting on a reboot. When an installer program needs to replace a file that's in use, it can't do that, so it places the file on the disk with a temporary name and places a value in the Windows Registry to indicate that the file needs to be renamed at the next reboot. Therefore, if you want to detect whether a given machine needs a reboot in order to complete the work of a previously-applied hotfix, patch, or software install, you can look at that value in the Registry to see if there's any work to be done on the next reboot. If there is, the machine needs a reboot. If there's nothing there, the machine doesn't need a reboot.
The Registry key you need to examine is a MultiString Value called, aptly enough "PendingFileRenameOperations" located on the following Registry path:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager
Below is a sample VBScript to perform a test of the local or a remote machine to see if a reboot is needed based on the PendingFileRenameOperations key. The script must be run with Administrator permission on the system to be checked. If run without Administrator permission, the script will be unable to connect with the remote machine and an error will be displayed.
When executed, the script prompts for the name of a PC on the network, which can be the PC you're using at the time. If no PC name is entered, the script aborts. Otherwise, it makes a Windows Management Instrumentation (WMI) call to the Registry provider on the remote machine and requests the value of the PendingFileRenameOperations key. If an actual value is found, this means that PC requires a reboot. If no value is found or the key isn't there, then the PC does not require a reboot. A message is displayed for the user indicating if the machine in question does or does not need to be rebooted.
I hope you'll find the script useful.
dim oReg
'
' Set a constant we'll use later
'
Const HKEY_LOCAL_MACHINE = &H80000002
'
' Ask the user for a PC name to check and abort if they
' don't give us one.
'
strComputer = InputBox("Which PC do you want to check?",_
"Reboot Need Checker")
if strComputer="" then
wscript.quit
end if
thePC = ltrim(rtrim(strComputer))
'
' Use the Windows Management Instrumentation (WMI) capability
' to connect to the remote computer's Registry provider.
'
on error resume next
set oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
If Err.Number <> 0 Then
MsgBox "Could not connect with WMI to PC " & strComputer & _
"'s Registry.", vbOKOnly, "ERROR!"
wscript.quit
End If
'
' Use the WMI Registry Provider to look up the reboot status in
' the remote PC's Registry. Display an error if we can't do it.
'
strvalue = "NOTHING"
strKeyPath = "SYSTEM\CurrentControlSet\Control\Session Manager"
strValueName = "PendingFileRenameOperations"
oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,_
strKeyPath,_
strValueName,_
arrValues
If Err.Number <> 0 Then
MsgBox "Could not read reboot status for the PC " & _
strComputer, vbOKOnly, "ERROR!"
wscript.quit
End If
'
' If arrValues returns a non-zero value below, then there are filenames in
' the PendingFileRenameOperations key, and therefore a reboot
' is needed to complete those rename operations.
'
if arrvalues > 0 then
msgbox strComputer & " requires a reboot at this time. ", _
vbokonly,"Reboot Needed"
else
msgbox strComputer & " does not require a reboot. ", _
vbokonly,"No Reboot Needed"
wscript.quit
end if