Tuesday, October 28, 2025

function Test-RebootRequired

 function Test-RebootRequired {

    <#

    .SYNOPSIS

    Checks for various indicators in the Windows registry and WMI that a system reboot is pending.


    .DESCRIPTION

    This function examines common registry keys and the CCM_ClientUtilities WMI class (if SCCM is present) 

    to determine if a Windows system requires a restart due to updates, component changes, or file renames.


    .OUTPUTS

    System.Boolean

        Returns $true if a reboot is pending, otherwise $false.

    #>

    [CmdletBinding()]

    param()


    # --- 1. Check Component Based Servicing (CBS) ---

    # Used by Windows Update for updates to core OS components

    if (Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -ErrorAction SilentlyContinue) {

        Write-Verbose "Reboot pending due to Component Based Servicing (CBS)."

        return $true

    }


    # --- 2. Check Windows Update Auto Update (WUAU) ---

    # Used by the Windows Update client

    if (Get-Item -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction SilentlyContinue) {

        Write-Verbose "Reboot required by Windows Update Auto Update (WUAU)."

        return $true

    }


    # --- 3. Check Pending File Rename Operations ---

    # Indicates that files need to be replaced on the next boot (common for patches)

    $PendingFileRenames = Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -ErrorAction SilentlyContinue

    if ($PendingFileRenames) {

        Write-Verbose "Reboot pending due to Pending File Rename Operations."

        return $true

    }


    # --- 4. Check SCCM/MEMCM Client Status (Optional) ---

    # Query the Configuration Manager client for its reboot status

    try {

        $SCCMRebootStatus = (Get-CimInstance -Namespace 'ROOT\ccm\ClientSDK' -ClassName 'CCM_ClientUtilities').DetermineIfRebootPending()

        if ($SCCMRebootStatus -ne $null -and $SCCMRebootStatus.RebootPending) {

            Write-Verbose "Reboot pending according to SCCM client."

            return $true

        }

    }

    catch {

        Write-Verbose "SCCM client WMI class not found or error occurred: $($_.Exception.Message)"

        # Continue if the SCCM check fails (not installed)

    }


    # If none of the checks returned true, no reboot is pending

    return $false

}


# --- Example Usage ---

if (Test-RebootRequired) {

    Write-Host "🚨 REBOOT IS PENDING. Server requires a restart to complete patching." -ForegroundColor Red

} else {

    Write-Host "✅ No reboot is pending. Patches appear to be complete." -ForegroundColor Green

}

No comments:

Post a Comment

precipitous loss of value by the Argentine Peso

The sustained, precipitous loss of value by the Argentine Peso is not a single event but the predictable outcome of chronic, deeply rooted m...