Function to detect pending reboot

Topics: Archive - Toolkit Extensions
Apr 17, 2014 at 11:12 PM
Edited Apr 21, 2014 at 4:20 AM
Requires SCCM Client (WMI class CCM_ClientUtilities must exist)
Function Test-PendingReboot{
<#
    .SYNOPSIS
        Check if the pending reboot exists. Returns $true or $false.
    .PARAMETER Computer
        Computer to detect if a pending reboot exists, default is local machine.
    .EXAMPLE
        Test-PendingReboot -Computer "PC0001"    
#>
    param(
        [Parameter(Mandatory=$false,Position=1, HelpMessage="Computer to run detection against")]
        [ValidateNotNullOrEmpty()]
        [String[]]
        $ComputerName = $env:COMPUTERNAME        
    )
    
    $reboot = [wmiclass]"\\$ComputerName\root\ccm\ClientSDK:CCM_ClientUtilities"
    $result = $reboot.DetermineIfRebootPending() | Select RebootPending
    $result.RebootPending
}
Coordinator
Apr 17, 2014 at 11:38 PM
Hi there,

Thanks for posting this script! Just one small suggestion in keeping with PowerShell verb-noun syntax, rename function to Test-PendingReboot.

Cheers,

Seán

Coordinator
Apr 18, 2014 at 3:56 PM
I have a suggestion! :) So this relies on the SCCM 07 / 12 agent being installed on a machine, and will fail if it's not.

An alternative would be to check for any pending file rename operations. Check for the existence of the following regkey:

HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\PendingFileRenameOperations

If it exists, a reboot is needed.

Hope this helps. Dan
May 15, 2014 at 3:40 PM
A much better place to look is in:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired

If the RebootRequired key exists, then one or more windows update needs a reboot.

This may be the thing that makes some MSIs balk and demand a reboot before doing anything.

(This is Windows 7, BTW)
Coordinator
May 20, 2014 at 1:21 PM
Actually this key only applies to windows updates. The PendingFileRenameOperations key applies to both updates and application installs - basically anything that might require a driver or system file replacement that can only be handled outside of the OS.
Jun 4, 2014 at 5:18 PM
Edited Jun 4, 2014 at 6:01 PM
The problem has already been solved here:

http://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542
PS C:\> Get-PendingReboot 
   
    Computer       : WKS01 
    CBServicing    : False 
    WindowsUpdate  : True 
    CCMClient      : False 
    PendFileRename : False 
    PendFileRenVal :  
    RebootPending  : True 
All we need to do is adapt it for our needs.

But if you want to decode PFRO we'll need to add this:
(FYI: That WhyReboot.exe utility is 32bit only)
$ComputerName="$env:COMPUTERNAME"
# Making registry connection to the local/remote computer
$RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine",$Computer)
                        
$RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
$RegValuePFRO = $RegSubKeySM.GetValue("PendingFileRenameOperations",$null)

$count=$RegValuePFRO.Count
If ($count % 2 -eq 0) {
    Echo "PendingFileRenameOperations value is consistent"
} Else {
    Echo "WARNING: PendingFileRenameOperations value is NOT consistent"
}

for ($index = 0; $index -lt $count; $index += 2) {
    write-host $RegValuePFRO[$index] -NoNewline
    
    If ($RegValuePFRO[$index+1] -eq "") { echo " will be DELETED"
    } Else {    echo " renamed to $( $RegValuePFRO[$index+1] )" }
}
PendingFileRenameOperations value is consistent
\??\C:\Windows\TEMP\mfe1D80.tmp\cleanup.exe will be DELETED
\??\C:\Windows\TEMP\mfe1D80.tmp will be DELETED
Jun 26, 2014 at 3:01 PM
If the pending reboot is true do you just reboot and the install will restart at next logon or do you have to trigger a defer? I am using SCCM 12.