From Scorching Rhinoceros, 2 Years ago, written in PowerShell.
Embed
  1. Function Get-PendingRebootStatus {
  2. <#
  3. .Synopsis
  4.     This will check to see if a server or computer has a reboot pending.
  5.     For updated help and examples refer to -Online version.
  6.  
  7. .NOTES
  8.     Name: Get-PendingRebootStatus
  9.     Author: theSysadminChannel
  10.     Version: 1.2
  11.     DateCreated: 2018-Jun-6
  12.  
  13. .LINK
  14.     https://thesysadminchannel.com/remotely-check-pending-reboot-status-powershell -
  15.  
  16.  
  17. .PARAMETER ComputerName
  18.     By default it will check the local computer.
  19.  
  20. .EXAMPLE
  21.     Get-PendingRebootStatus -ComputerName PAC-DC01, PAC-WIN1001
  22.  
  23.     Description:
  24.     Check the computers PAC-DC01 and PAC-WIN1001 if there are any pending reboots.
  25. #>
  26.  
  27.     [CmdletBinding()]
  28.     Param (
  29.         [Parameter(
  30.             Mandatory = $false,
  31.             ValueFromPipeline = $true,
  32.             ValueFromPipelineByPropertyName = $true,
  33.             Position=0
  34.         )]
  35.  
  36.     [string[]]  $ComputerName = $env:COMPUTERNAME
  37.     )
  38.  
  39.  
  40.     BEGIN {}
  41.  
  42.     PROCESS {
  43.         Foreach ($Computer in $ComputerName) {
  44.             Try {
  45.                 $PendingReboot = $false
  46.  
  47.                 $HKLM = [UInt32] "0x80000002"
  48.                 $WMI_Reg = [WMIClass] "\$Computer\root\default:StdRegProv"
  49.  
  50.                 if ($WMI_Reg) {
  51.                     if (($WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\")).sNames -contains 'RebootPending') {$PendingReboot = $true}
  52.                     if (($WMI_Reg.EnumKey($HKLM,"SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\")).sNames -contains 'RebootRequired') {$PendingReboot = $true}
  53.  
  54.                     #Checking for SCCM namespace
  55.                     $SCCM_Namespace = Get-WmiObject -Namespace ROOT\CCM\ClientSDK -List -ComputerName $Computer -ErrorAction Ignore
  56.                     if ($SCCM_Namespace) {
  57.                         if (([WmiClass]"\$Computer\ROOT\CCM\ClientSDK:CCM_ClientUtilities").DetermineIfRebootPending().RebootPending -eq $true) {$PendingReboot = $true}
  58.                     }
  59.  
  60.                     [PSCustomObject]@{
  61.                         ComputerName  = $Computer.ToUpper()
  62.                         PendingReboot = $PendingReboot
  63.                     }
  64.                 }
  65.             } catch {
  66.                 Write-Error $_.Exception.Message
  67.  
  68.             } finally {
  69.                 #Clearing Variables
  70.                 $null = $WMI_Reg
  71.                 $null = $SCCM_Namespace
  72.             }
  73.         }
  74.     }
  75.  
  76.     END {}
  77. }