Test-PendingReboot.ps1


<#PSScriptInfo
 
.VERSION 1.2
 
.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
 
.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
    {
        foreach ($computer in $ComputerName) {
            $connParams = @{
                'ComputerName' = $computer
            }
            if ($PSBoundParameters.ContainsKey('Credential')) {
                $connParams.Credential = $Credential
            }

            $output = @{
                ComputerName = $computer
                IsPendingReboot = $false
            }

            $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 -and (Invoke-Command @connParams -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing' -Name 'RebootPending' -ErrorAction SilentlyContinue }))
            {
                Write-Verbose -Message 'Reboot pending detected in the Component Based Servicing registry key'
                $output.IsPendingReboot = $true
                [pscustomobject]$output
            } elseif (Invoke-Command @connParams -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update' -Name 'RebootRequired' -ErrorAction SilentlyContinue }) {
                Write-Verbose -Message 'WUAU has a reboot pending'
                $output.IsPendingReboot = $true
                [pscustomobject]$output
            } elseif ($fileNameOp = Invoke-Command @connParams -ScriptBlock { Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Name 'PendingFileRenameOperations' -ErrorAction SilentlyContinue }) {
                if ($PendingReboot.PendingFileRenameOperations) {
                    Write-Verbose -Message 'Reboot pending in the PendingFileRenameOperations registry value'
                    $output.IsPendingReboot = $true
                    [pscustomobject]$output
                } else {
                    [pscustomobject]$output
                }
            } else {
                [pscustomobject]$output
            }
        }
    }
    catch
    {
        Write-Error -Message $_.Exception.Message
    }
}