Windows - Get Pending Reboot

Octopus.Script exported 2014-05-30 by bobjwalker belongs to ‘Windows’ category.

Get pending reboot status from computer.

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Computer name

ComputerName

Computer Name to get pending reboot status from.

Script body

Steps based on this template will execute the following PowerShell script.

$TempErrAct = $ErrorActionPreference 
    $ErrorActionPreference = "Stop"   
    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) 
           
            # If any of the variables are true, set $Pending variable to $true 
            If ($CBSRebootPend -or $WUAURebootReq -or $PendFileRename) 
              { 
                $Pending = $true 
              }#End If ($CBS -or $WUAU -or $PendFileRename) 
            # Creating Custom PSObject and Select-Object Splat 
			$SelectSplat = @{ 
			    Property=('Computer','CBServicing','WindowsUpdate','PendFileRename','PendFileRenVal','RebootPending') 
			    } 
            New-Object -TypeName PSObject -Property @{ 
                Computer=$WMI_OS.CSName 
                CBServicing=$CBSRebootPend 
                WindowsUpdate=$WUAURebootReq 
                PendFileRename=$PendFileRename 
                PendFileRenVal=$RegValuePFRO 
                RebootPending=$Pending 
                } | Select-Object @SelectSplat 
          }#End Try 
        Catch 
          { 
            Write-Warning "$Computer`: $_" 
            # 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) 
	  $ErrorActionPreference = $TempErrAct 

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "814c4265-62ec-44d3-9d42-800aef8f7380",
  "Name": "Windows - Get Pending Reboot",
  "Description": "Get pending reboot status from computer.",
  "Version": 1,
  "ExportedAt": "2014-05-30T20:34:01.920+00:00",
  "ActionType": "Octopus.Script",
  "Author": "bobjwalker",
  "Parameters": [
    {
      "Name": "ComputerName",
      "Label": "Computer name",
      "HelpText": "Computer Name to get pending reboot status from.",
      "DefaultValue": null,
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptBody": "$TempErrAct = $ErrorActionPreference \n    $ErrorActionPreference = \"Stop\"   \n    Foreach ($Computer in $ComputerName) \n      { \n        Try \n          { \n            # Setting pending values to false to cut down on the number of else statements \n            $PendFileRename,$Pending,$SCCM = $false,$false,$false \n\t\t\t\n            # Setting CBSRebootPend to null since not all versions of Windows has this value \n            $CBSRebootPend = $null \n\t\t\t\n            # Querying WMI for build version \n            $WMI_OS = Get-WmiObject -Class Win32_OperatingSystem -Property BuildNumber, CSName -ComputerName $Computer \n\t\t\t\n            # Making registry connection to the local/remote computer \n            $RegCon = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]\"LocalMachine\",$Computer) \n\t\t\t\n            # If Vista/2008 & Above query the CBS Reg Key \n            If ($WMI_OS.BuildNumber -ge 6001) \n              { \n                $RegSubKeysCBS = $RegCon.OpenSubKey(\"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\\").GetSubKeyNames() \n                $CBSRebootPend = $RegSubKeysCBS -contains \"RebootPending\" \n              }#End If ($WMI_OS.BuildNumber -ge 6001) \n\t\t\t  \n            # Query WUAU from the registry \n            $RegWUAU = $RegCon.OpenSubKey(\"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\\") \n            $RegWUAURebootReq = $RegWUAU.GetSubKeyNames() \n            $WUAURebootReq = $RegWUAURebootReq -contains \"RebootRequired\" \n\t\t\t\n            # Query PendingFileRenameOperations from the registry \n            $RegSubKeySM = $RegCon.OpenSubKey(\"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\\") \n            $RegValuePFRO = $RegSubKeySM.GetValue(\"PendingFileRenameOperations\",$null) \n\t\t\t\n            # Closing registry connection \n            $RegCon.Close() \n\t\t\t\n            # If PendingFileRenameOperations has a value set $RegValuePFRO variable to $true \n            If ($RegValuePFRO) \n              { \n                $PendFileRename = $true \n              }#End If ($RegValuePFRO) \n           \n            # If any of the variables are true, set $Pending variable to $true \n            If ($CBSRebootPend -or $WUAURebootReq -or $PendFileRename) \n              { \n                $Pending = $true \n              }#End If ($CBS -or $WUAU -or $PendFileRename) \n            # Creating Custom PSObject and Select-Object Splat \n\t\t\t$SelectSplat = @{ \n\t\t\t    Property=('Computer','CBServicing','WindowsUpdate','PendFileRename','PendFileRenVal','RebootPending') \n\t\t\t    } \n            New-Object -TypeName PSObject -Property @{ \n                Computer=$WMI_OS.CSName \n                CBServicing=$CBSRebootPend \n                WindowsUpdate=$WUAURebootReq \n                PendFileRename=$PendFileRename \n                PendFileRenVal=$RegValuePFRO \n                RebootPending=$Pending \n                } | Select-Object @SelectSplat \n          }#End Try \n        Catch \n          { \n            Write-Warning \"$Computer`: $_\" \n            # If $ErrorLog, log the file to a user specified location/path \n            If ($ErrorLog) \n              { \n                Out-File -InputObject \"$Computer`,$_\" -FilePath $ErrorLog -Append \n              }#End If ($ErrorLog) \n          }#End Catch \n      }#End Foreach ($Computer in $ComputerName) \n\t  $ErrorActionPreference = $TempErrAct ",
    "Octopus.Action.Script.Syntax": "PowerShell"
  },
  "Category": "Windows",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-get-pending-reboot.json",
  "Website": "/step-templates/814c4265-62ec-44d3-9d42-800aef8f7380",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////Da3qSsLvhtb0wur6O7zuWcfxldv2aMzyK7ftpOD3s+X48Pr+0fD7d9HzHLLr4fX8xD/OcwAAAaNJREFUeNrs3cFygjAUQFECWott1f//2sJoW6kIKEzNs+euXOmcmSSGDa8oJEmSJEmSJGmsj1W1K9cpsGD1Vr2WdToVEPC+2lYvZfpVrEW0qZpF1F+MRdRugzoNlvkiarfBPk0pT8GhWUSX2yASpDlLr2+DEJBmEY1ug6whx7N0n2b30G1QlmmxHsRYp6X76yvF9vg5RYQczq8UVURI35UiFmTgShED0p6lI1eKzCHTrxS5Qk6PZ9PLDtJ9PIsJmXWlyAky6/dAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQMJCyjltF/iO3gpJUpD8s4OAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgID8T8itwwKyhbTdMr4ha8hXUwZqhICcOgyNOIkE+V5wo4MSgr1u/fp7poO+AL8K/gL8yw0UeyRB34m9iQ/pVD8L5JYTO3NI58R+AsiEEzsW5OfE3sUe/zRwYkeGnG2g2CPS7rhjF4GKP0ZwyoldxK37kFqEL/7wU0mSJEmSJOmJ+xRgAHxZTCXGdZkfAAAAAElFTkSuQmCC",
  "$Meta": {
    "Type": "ActionTemplate"
  }
}

History

Page updated on Friday, May 30, 2014