Test-PendingReboot.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID fe3d3698-52fc-40e8-a95c-bbc67a507ed1
 
.AUTHOR Adam Bertram
 
.COMPANYNAME Adam the Automator, LLC
 
.COPYRIGHT
 
.DESCRIPTION This function tests various registry values to see if the local computer is pending a reboot.
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
#>

param()

function Test-PendingReboot
{
    <#
        .SYNOPSIS
            This function tests various registry values to see if the local computer is pending a reboot
        .NOTES
            Inspiration from: https://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542
        .EXAMPLE
            PS> Test-PendingReboot
             
            This example checks various registry values to see if the local computer is pending a reboot.
    #>

    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]$ComputerName,
        
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [pscredential]$Credential
    )
    process {
        $ErrorActionPreference = 'Stop'
        try
        {
            $connParams = @{
                'ComputerName' = $ComputerName
            }
            if ($PSBoundParameters.ContainsKey('Credential')) {
                $connParams.Credential = $Credential
            }

            $cimSession = New-CimSession @connParams
            $OperatingSystem = Get-CimInstance -CimSession $cimSession -ClassName Win32_OperatingSystem -Property BuildNumber, CSName
            
            # If Vista/2008 & Above query the CBS Reg Key
            If ($OperatingSystem.BuildNumber -ge 6001)
            {
                $PendingReboot = Invoke-Command @connParams -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing' -Name 'RebootPending' -ErrorAction SilentlyContinue }
                if ($PendingReboot)
                {
                    Write-Verbose -Message 'Reboot pending detected in the Component Based Servicing registry key'
                    return $true
                }
            }
            
            # Query WUAU from the registry
            $PendingReboot = Invoke-Command @connParams -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update' -Name 'RebootRequired' -ErrorAction SilentlyContinue }
            if ($PendingReboot)
            {
                Write-Verbose -Message 'WUAU has a reboot pending'
                return $true
            }
            
            # Query PendingFileRenameOperations from the registry
            $PendingReboot = Invoke-Command @connParams -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Name 'PendingFileRenameOperations' -ErrorAction SilentlyContinue }
            if ($PendingReboot -and $PendingReboot.PendingFileRenameOperations)
            {
                Write-Verbose -Message 'Reboot pending in the PendingFileRenameOperations registry value'
                return $true
            }
            $false
        }
        catch
        {
            Write-Error -Message $_.Exception.Message
        }
    }
}