#PowerShell script to Check for Pending Reboot on Remote Computer(s)

Here is a #PowerShell script to Check for Pending Reboot on Remote Computer(s). It is also accompanied with logging in CMTRACE format.

The Check Pending Reboot is based on the following parameters conditions

  •  CBServicing = Component Based Servicing (Windows ALL)
  • WindowsUpdate = Windows Update / Auto Update (Windows ALL)
  • CCMClientSDK = SCCM 2012 Clients only (DetermineIfRebootPending method)
  • PendFileRename = PendingFileRenameOperations (Windows ALL)

Hers is the Full Script

<#
    .SYNOPSIS
        checks for pending operations that need a reboot
 .Return values:
         0: no reboot needed
         1: reboot needed
        99: fatal error (see error description in logfiles)
#>




###############################################################################

Function LogIt
{
  Param (
  [Parameter(Mandatory=$True)]
  $Message,
  [Parameter(Mandatory=$True)]
  $Component,
  [Parameter(Mandatory=$True)]
  $Type )

  switch ($Type)
  {
    1 { $Type = "Info" }
    2 { $Type = "Warning" }
    3 { $Type = "Error" }
    4 { $Type = "Verbose" }
  }

  if (($Type -eq "Verbose") -and ($Global:Verbose))
  {
    $ToLog = "{0} `$$<{1}><{2} {3}><thread={4}>" -f ($Type + ":" + $Message), ($Global:ScriptName + " : " + $Component), (Get-Date -Format "MM-dd-yyyy"), (Get-Date -Format "HH:mm:ss.ffffff"), $pid
    $ToLog | Out-File -Append -Encoding UTF8 -FilePath ("filesystem::{0}" -f $Global:LogFile)
    Write-Host $Message
  }
  elseif ($Type -ne "Verbose")
  {
    $ToLog = "{0} `$$<{1}><{2} {3}><thread={4}>" -f ($Type + ":" + $Message), ($Global:ScriptName + " : " + $Component), (Get-Date -Format "MM-dd-yyyy"), (Get-Date -Format "HH:mm:ss.ffffff"), $pid
    $ToLog | Out-File -Append -Encoding UTF8 -FilePath ("filesystem::{0}" -f $Global:LogFile)
    Write-Host $Message
  }
  if (($Type -eq 'Warning') -and ($Global:ScriptStatus -ne 'Error')) { $Global:ScriptStatus = $Type }
  if ($Type -eq 'Error') { $Global:ScriptStatus = $Type }

  if ((Get-Item $Global:LogFile).Length/1KB -gt $Global:MaxLogSizeInKB)
  {
    $log = $Global:LogFile
    Remove-Item ($log.Replace(".log", ".lo_"))
    Rename-Item $Global:LogFile ($log.Replace(".log", ".lo_")) -Force
  }
}

$component = "CheckRebootPending"
$file = "$env:ComputerName"
$Hostname = "$Env:computername"
$VerboseLogging = "true"
[bool]$Global:Verbose = [System.Convert]::ToBoolean($VerboseLogging)
$Global:LogFile = "$env:Windir\Debug\$component.log"
$Global:MaxLogSizeInKB = 10240
$Global:ScriptName = 'LogIt.ps1' 
$Global:ScriptStatus = 'Success'

###############################################################################

Function Get-PendingReboot
{
<#
.SYNOPSIS
    Gets the pending reboot status on a local or remote computer.

.DESCRIPTION
    This function will query the registry on a local or remote computer and determine if the
    system is pending a reboot, from either Microsoft Patching or a Software Installation.
    For Windows 2008+ the function will query the CBS registry key as another factor in determining
    pending reboot state.  "PendingFileRenameOperations" and "Auto Update\RebootRequired" are observed
    as being consistent across ALL Windows Platforms.
 
    CBServicing = Component Based Servicing (Windows ALL)
    WindowsUpdate = Windows Update / Auto Update (Windows ALL)
    CCMClientSDK = SCCM 2012 Clients only (DetermineIfRebootPending method) otherwise $null value
    PendFileRename = PendingFileRenameOperations (Windows ALL)

.PARAMETER ComputerName
    A single Computer or an array of computer names.  The default is localhost ($env:COMPUTERNAME).

.PARAMETER ErrorLog
    A single path to send error data to a log file.

.EXAMPLE
    PS C:\> Get-PendingReboot -ComputerName (Get-Content C:\ServerList.txt) | Format-Table -AutoSize
 
    Computer CBServicing WindowsUpdate CCMClientSDK PendFileRename PendFileRenVal RebootPending
    -------- ----------- ------------- ------------ -------------- -------------- -------------
    DC01           False         False                       False                        False
    DC02           False         False                       False                        False
    FS01           False         False                       False                        False

    This example will capture the contents of C:\ServerList.txt and query the pending reboot
    information from the systems contained in the file and display the output in a table. The
    null values are by design, since these systems do not have the SCCM 2012 client installed,
    nor was the PendingFileRenameOperations value populated.

.EXAMPLE
    PS C:\> Get-PendingReboot
 
    Computer       : WKS01
    CBServicing    : False
    WindowsUpdate  : True
    CCMClient      : False
    PendFileRename : False
    PendFileRenVal : 
    RebootPending  : True
 
    This example will query the local machine for pending reboot information.
 
.EXAMPLE
    PS C:\> $Servers = Get-Content C:\Servers.txt
    PS C:\> Get-PendingReboot -Computer $Servers | Export-Csv C:\PendingRebootReport.csv -NoTypeInformation
 
    This example will create a report that contains pending reboot information.

.LINK
    Component-Based Servicing:
    http://technet.microsoft.com/en-us/library/cc756291(v=WS.10).aspx
 
    PendingFileRename/Auto Update:
    http://support.microsoft.com/kb/2723674
    http://technet.microsoft.com/en-us/library/cc960241.aspx
    http://blogs.msdn.com/b/hansr/archive/2006/02/17/patchreboot.aspx

    SCCM 2012/CCM_ClientSDK:
    http://msdn.microsoft.com/en-us/library/jj902723.aspx

.NOTES

#>
 
 [CmdletBinding()]
 param (
  [Parameter(Position = 0, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
  [Alias("CN", "Computer")]
  [String[]]$ComputerName = "$env:COMPUTERNAME",
  [String]$ErrorLog
 )
 
 Begin
 {
  # Adjusting ErrorActionPreference to stop on all errors, since using [Microsoft.Win32.RegistryKey]
  # does not have a native ErrorAction Parameter, this may need to be changed if used within another
  # function.
  $TempErrAct = $ErrorActionPreference
  $ErrorActionPreference = "Stop"
 }#End Begin Script Block
 Process
 {
  Foreach ($Computer in $ComputerName)
  {
   Try
   {
    # Setting pending values to false to cut down on the number of else statements
    $PendFileRename, $Pending, $SCCM = $false, $false, $false
    
    # Setting CBSRebootPend to null since not all versions of Windows has this value
    $CBSRebootPend = $null
    
    # Querying WMI for build version
    $WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -Property BuildNumber, CSName -ComputerName $Computer
    
    # Making registry connection to the local/remote computer
    $RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]"LocalMachine", $Computer)
    
    # If Vista/2008 & Above query the CBS Reg Key
    If ($WMI_OS.BuildNumber -ge 6001)
    {
     $RegSubKeysCBS = $RegCon.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\").GetSubKeyNames()
     $CBSRebootPend = $RegSubKeysCBS -contains "RebootPending"
     
    }#End If ($WMI_OS.BuildNumber -ge 6001)
    
    # Query WUAU from the registry
    $RegWUAU = $RegCon.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")
    $RegWUAURebootReq = $RegWUAU.GetSubKeyNames()
    $WUAURebootReq = $RegWUAURebootReq -contains "RebootRequired"
    
    # Query PendingFileRenameOperations from the registry
    $RegSubKeySM = $RegCon.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\")
    $RegValuePFRO = $RegSubKeySM.GetValue("PendingFileRenameOperations", $null)
    
    # Closing registry connection
    $RegCon.Close()
    
    # If PendingFileRenameOperations has a value set $RegValuePFRO variable to $true
    If ($RegValuePFRO)
    {
     $PendFileRename = $true
     
    }#End If ($RegValuePFRO)
    
    # Determine SCCM 2012 Client Reboot Pending Status
    # To avoid nested 'if' statements and unneeded WMI calls to determine if the CCM_ClientUtilities class exist, setting EA = 0
    $CCMClientSDK = $null
    $CCMSplat = @{
     NameSpace = 'ROOT\ccm\ClientSDK'
     Class = 'CCM_ClientUtilities'
     Name = 'DetermineIfRebootPending'
     ComputerName = $Computer
     ErrorAction = 'SilentlyContinue'
    }
    $CCMClientSDK = Invoke-WmiMethod @CCMSplat
    If ($CCMClientSDK)
    {
     If ($CCMClientSDK.ReturnValue -ne 0)
     {
      LogIt -message "DetermineIfRebootPending returned error code $($CCMClientSDK.ReturnValue)" -component $component -type 3
      #Write-Warning "Error: DetermineIfRebootPending returned error code $($CCMClientSDK.ReturnValue)"
      
     }#End If ($CCMClientSDK -and $CCMClientSDK.ReturnValue -ne 0)
     
     If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending)
     {
      $SCCM = $true
      
     }#End If ($CCMClientSDK.IsHardRebootPending -or $CCMClientSDK.RebootPending)
     
    }#End If ($CCMClientSDK)
    Else
    {
     $SCCM = $null
     
    }
    
    # If any of the variables are true, set $Pending variable to $true
    If ($CBSRebootPend -or $WUAURebootReq -or $SCCM -or $PendFileRename)
    {
     $Pending = $true
     
    }#End If ($CBS -or $WUAU -or $PendFileRename)
    
    # Creating Custom PSObject and Select-Object Splat
    $SelectSplat = @{
     #                            Property=('Computer','CBServicing','WindowsUpdate','CCMClientSDK','PendFileRename','PendFileRenVal','RebootPending')
     Property = ('Computer', 'CBServicing', 'WindowsUpdate', 'CCMClientSDK', 'PendFileRename', 'RebootPending')
    }
    New-Object -TypeName PSObject -Property @{
     Computer = $WMI_OS.CSName
     CBServicing = $CBSRebootPend
     WindowsUpdate = $WUAURebootReq
     CCMClientSDK = $SCCM
     PendFileRename = $PendFileRename
     #PendFileRenVal=$RegValuePFRO
     RebootPending = $Pending
    } | Select-Object @SelectSplat
    
   }#End Try
   
   Catch
   {
    #Write-Warning "$Computer`: $_"
    LogIt -message "$Computer`: $_" -component $component -type 2
    
    # If $ErrorLog, log the file to a user specified location/path
    #      If ($ErrorLog)
    #       {
    #        Out-File -InputObject "$Computer`,$_" -FilePath $ErrorLog -Append
    #       }#End If ($ErrorLog)
    
   }#End Catch
   
  }#End Foreach ($Computer in $ComputerName)
  
 }#End Process
 
 End
 {
  # Resetting ErrorActionPref
  $ErrorActionPreference = $TempErrAct
 }#End End
 
}#End Function

LogIt -message " ################################### $component ###################################" -component $component -type 1
$error.clear()

try
{
 $ret = Get-PendingReboot
 
 if ($ret)
 {
  LogIt -message " $ret" -component $component -type 1
  If ($ret -match "=true")
  {
   LogIt -message " Reboot necessary: Yes - ExitCode 1" -component $component -type 1
   LogIt -message " ################################### Exit $component ###################################" -component $component -type 1
   Exit 1
   
  }
  else
  {
   LogIt -message " Reboot necessary: No - ExitCode 0" -component $component -type 1
   LogIt -message " ################################### Exit $component ###################################" -component $component -type 1
   Exit 0
  }
 }
 else
 {
  LogIt -message " Error: $error[0]" -component $component -type 3
  LogIt -message " ################################### Exit $component ###################################" -component $component -type 3
  Exit 99
 }
}

catch
{
 LogIt -message " Error: $error[0]" -component $component -type 3
 LogIt -message " ################################### Exit $component ###################################" -component $component -type 3
 Exit 99
}

Cheers 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: