Check Pending Reboots from Jenkins - JoelRochambeau/Powershell GitHub Wiki
<# .Synopsis This script checks the systems registry keys to determine if the system is pending a reboot. .DESCRIPTION This script is intended to check the status existing registry keys from a given endpoint. Passing arguments to the script is required for System Hostnames, this is done with the configuration file as a string.
.EXAMPLE Get-Pending-Reboot.ps1 -ConfigurationFilePath ".\svc_sp_pltscripts.txt"
.NOTES Originally written by Joel Rochambeau on 1/3/2018 Contributers: Jason Ralston, and Jarom Mills #>
[CmdletBinding()] Param( #Specifies the path to the configuration file. If there is no path listed, you will be prompted to enter one. [Parameter(Position=0, Mandatory=$true)] [String]$ConfigurationFilePath )
[int]$ExitCode = 0
#Check for config file $confFileExists = Test-Path -Path $configFilePath;
[string[]]$checkConf = ""; if ($confFileExists) {
$ServerNames = Get-Content -Path $(Resolve-Path -Relative $configFilePath);
} else { Write-Error "Cannot find configuration file: $configFilePath`nExiting..."; Exit 1; }
#Argument from Jenkins to pass the SeverNames.
#Credential configuration required for Jenkins to pass the Server Account# $pass=$ENV:Password | ConvertTo-SecureString -asPlainText -Force $credential = New-Object System.Management.Automation.PSCredential($ENV:UserName,$pass)
#Resets Array to null and Initializes $ServerRebootStatus = @()
#--------------------------------End Variables --------------------#
#For each computer in the server argument passed from jenkins Foreach ($Computer in $ServerNames) { write-host "$Computer Has been Checked"
#Perform remote access via Invoke to perform regkey checks, if regkey exists value returned is either false or true
$ServerRebootCheck = Invoke-Command -ComputerName $Computer -Credential $credential -ScriptBlock {
write-host "System Has been Logged Into"
#Check to see if the RebootPending key exists
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { return $true }
#Check to see if the RebootRequired key exists
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { return $true }
#Check to see if the key PendingFileRenameOperations exists
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return $true}
try {
$util = [wmiclass]"\.\root\ccm\clientsdk:CCM_ClientUtilities"
$status = $util.DetermineIfRebootPending()
if(($status -ne $null) -and $status.RebootPending){
return $true
} #End If
}catch{} #Not setup for the try catch yet
return $false } #End Invoke #Append each of the computer object checks to a string array $ServerRebootStatus += "Hostname:" + $Computer + " Pending:" + $ServerRebootCheck +" | " } #End Foreach Write-host $ServerRebootStatus #Check the status to see if any system was flagged as true and then check to see if the check is running against test or development servers If ($ServerRebootStatus -match "True" -AND $ServerRebootStatus -match "WP" ){ $ProdServersIdentifiedWithIssue = $ServerRebootStatus | Select-string "True" $ExitCode = 1
$ProdBody ="<b>PRODUCTION REBOOT PENDING STATUS:</b>" + " " + "</br><b> $ProdServersIdentifiedWithIssue </b> " +"</br></br> Please use pre-approved CAB 5062 for Production Server Reboots and reboot ASAP" +"</br></br></br> <h2>Follow instuctions below for creating Pre-Appproved Cab</h2> </br>
1. Create an incident in service desk using OIS-CSS Incident management</br>
2. Create a CAB referencing the incident number and the CAB 5062 Go to Change requests and find Request for Preapproval that you are wanting to clone. Click on the …(Ellipse) Select workflows and then select clone change request. Give it about 1 minute and there should be a new request on the page with section 1 complete and section 3B populated. Needs window and incident populated to save and get a number.</br>
3. When you have create the CAB get manager and Team Lead approval sign off (I.E… Forward the cab number to your Team Lead and Manager asking them to check the boxes)</br>
4. The Server/Service re-boot can now be performed</br>
5. Standard communications processes will be followed for success/failure/availability following reboot.</br>
6. The CAB request section 4 will need to be completed indicating ready to close</br>
7. Close the incident and send an email to OIS-CSS Incident management alerting them to the change being completed.</br>
</br></br>"
}#End IF
#Check the status to see if any system was flagged as true and then check to see if the check is running against test or development servers If ($ServerRebootStatus -match "True" -AND ($ServerRebootStatus -match "WD" -OR $ServerReBootStatus -match "WT")){ $DevServersIdentifiedWithIssue = $ServerRebootStatus | Select-string "True" $ExitCode = 1 $DevBody = "DEVOPMENT/TEST REBOOT PENDING STATUS:" + " " + " $DevServersIdentifiedWithIssue " +" Development or Test Server Reboot is required ASAP" +"Consult End Users If Required for Service Interruption "
} #End IF
#If any server is identified with True within the string send an email to the individuals identified, append the email body relevant to the Prod/Test/Dev to the email. If ($ServerRebootStatus -match "True"){
Write-Host "Server Identified as failed on the Pending Reboot Check.
Please use pre-approved CAB 5062 for Server Reboots and reboot ASAP
Follow instuctions below for creating Pre-Appproved Cab
1. Create an incident in service desk using OIS-CSS Incident management
2. Create a CAB referencing the incident number and the CAB 5062 Go to Change requests and find Request for Preapproval that you are wanting to clone. Click on the …(Ellipse) Select workflows and then select clone change request. Give it about 1 minute and there should be a new request on the page with section 1 complete and section 3B populated. Needs window and incident populated to save and get a number.
3. When you have create the CAB get manager and Team Lead approval sign off (I.E… Forward the cab number to your Team Lead and Manager asking them to check the boxes)
4. When the change is completed, complete section 4 of the CAB Request you created in step 2
5. Close the incident and send an email to OIS-CSS Incident management alerting them to the change being completed.
"
$EmailSignature = "Thank You, Enterprise Platform Services"
#Also, Email the contents above to the following Individuals
write-host "Emailing : $ENV:Recipients"
$recipients = $ENV:Recipients
$recipients = $recipients.split(",")
Send-MailMessage -SMTPServer smtp.dhsoha.state.or.us -To $recipients -From [email protected] -Subject "Pending Reboot: True" -Body "$ProdBody + $DevBody + $EmailSignature" -BodyAsHtml
} # End IF