Octopus.Script exported 2020-01-14 by DevOpsDerek belongs to ‘Windows’ category.
Checks if Server is pending a reboot, and reboots if it needs it.
Script body
Steps based on this template will execute the following PowerShell script.
# Sourced from https://github.com/adbertram/Random-PowerShell-Work/blob/master/Random%20Stuff/Test-PendingReboot.ps1
# Script will run only on the server with the Tentacle installed
function Test-RegistryKey($Key) {
if (Get-Item -Path $Key -ErrorAction Ignore) {
return $true
}
return $false
}
function Test-RegistryValue($Key, $Value) {
if (Get-ItemProperty -Path $Key -Name $Value -ErrorAction Ignore) {
return $true
}
return $false
}
function Test-RegistryValueNotNull($Key, $Value) {
if (($regVal = Get-ItemProperty -Path $Key -Name $Value -ErrorAction Ignore) -and $regVal.($Value)) {
return $true
}
return $false
}
$IsPendingReboot = $false
# Testing Registry keys for Restarts
# an exception is thrown when Get-ItemProperty or Get-ChildItem are passed a nonexistant key path
$tests = @(
{ Test-RegistryKey -Key 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending' }
{ Test-RegistryKey -Key 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootInProgress' }
{ Test-RegistryKey -Key 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired' }
{ Test-RegistryKey -Key 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\PackagesPending' }
{ Test-RegistryKey -Key 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\PostRebootReporting' }
{ Test-RegistryValueNotNull -Key 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Value 'PendingFileRenameOperations' }
{ Test-RegistryValueNotNull -Key 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager' -Value 'PendingFileRenameOperations2' }
{
(Test-RegistryKey -Key 'HKLM:\SOFTWARE\Microsoft\Updates') -and
(Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Updates' -Name 'UpdateExeVolatile' | Select-Object -ExpandProperty UpdateExeVolatile) -ne 0
}
{ Test-RegistryValue -Key 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce' -Value 'DVDRebootSignal' }
{ Test-RegistryKey -Key 'HKLM:\SOFTWARE\Microsoft\ServerManager\CurrentRebootAttemps' }
{ Test-RegistryValue -Key 'HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon' -Value 'JoinDomain' }
{ Test-RegistryValue -Key 'HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon' -Value 'AvoidSpnSet' }
{
(Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName').ComputerName -ne
(Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName').ComputerName
}
{
if (Get-ChildItem -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Services\Pending') {
$true
}
}
)
Write-Debug "Running tests to checking for reboot pending"
foreach ($test in $tests) {
if (& $test) {
Write-Debug "Test found reboot pending: $test"
$IsPendingReboot = $true
break
}
}
if($IsPendingReboot -eq $true) {
Write-Host "Restarting"
Restart-Computer -Force
}
if($IsPendingReboot -eq $false) {
Write-Host "No Restart required"
}
Provided under the Apache License version 2.0.
To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.
{
"Id": "cb0ca23c-524d-44a9-86e0-2ae05989d6a0",
"Name": "Check for Pending Restart and Restart if required",
"Description": "Checks if Server is pending a reboot, and reboots if it needs it.",
"Version": 0,
"ExportedAt": "2020-01-14T08:41:32.508Z",
"ActionType": "Octopus.Script",
"Author": "DevOpsDerek",
"Packages": [],
"Parameters": [],
"Properties": {
"Octopus.Action.Script.ScriptSource": "Inline",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptBody": "# Sourced from https://github.com/adbertram/Random-PowerShell-Work/blob/master/Random%20Stuff/Test-PendingReboot.ps1\n# Script will run only on the server with the Tentacle installed\n\nfunction Test-RegistryKey($Key) {\n if (Get-Item -Path $Key -ErrorAction Ignore) {\n return $true\n }\n return $false\n}\n\nfunction Test-RegistryValue($Key, $Value) {\n if (Get-ItemProperty -Path $Key -Name $Value -ErrorAction Ignore) {\n return $true\n }\n return $false\n}\n\nfunction Test-RegistryValueNotNull($Key, $Value) {\n if (($regVal = Get-ItemProperty -Path $Key -Name $Value -ErrorAction Ignore) -and $regVal.($Value)) {\n return $true\n }\n return $false\n}\n\n$IsPendingReboot = $false\n\n# Testing Registry keys for Restarts\n# an exception is thrown when Get-ItemProperty or Get-ChildItem are passed a nonexistant key path\n$tests = @(\n { Test-RegistryKey -Key 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\RebootPending' }\n { Test-RegistryKey -Key 'HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\RebootInProgress' }\n { Test-RegistryKey -Key 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\RebootRequired' }\n { Test-RegistryKey -Key 'HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\PackagesPending' }\n { Test-RegistryKey -Key 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\PostRebootReporting' }\n { Test-RegistryValueNotNull -Key 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Session Manager' -Value 'PendingFileRenameOperations' }\n { Test-RegistryValueNotNull -Key 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\Session Manager' -Value 'PendingFileRenameOperations2' }\n { \n\t\t(Test-RegistryKey -Key 'HKLM:\\SOFTWARE\\Microsoft\\Updates') -and\n\t\t(Get-ItemProperty -Path 'HKLM:\\SOFTWARE\\Microsoft\\Updates' -Name 'UpdateExeVolatile' | Select-Object -ExpandProperty UpdateExeVolatile) -ne 0 \n\t}\n { Test-RegistryValue -Key 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce' -Value 'DVDRebootSignal' }\n { Test-RegistryKey -Key 'HKLM:\\SOFTWARE\\Microsoft\\ServerManager\\CurrentRebootAttemps' }\n { Test-RegistryValue -Key 'HKLM:\\SYSTEM\\CurrentControlSet\\Services\\Netlogon' -Value 'JoinDomain' }\n { Test-RegistryValue -Key 'HKLM:\\SYSTEM\\CurrentControlSet\\Services\\Netlogon' -Value 'AvoidSpnSet' }\n {\n (Get-ItemProperty -Path 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName').ComputerName -ne\n (Get-ItemProperty -Path 'HKLM:\\SYSTEM\\CurrentControlSet\\Control\\ComputerName\\ComputerName').ComputerName\n }\n {\n if (Get-ChildItem -Path 'HKLM:\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Services\\Pending') {\n $true\n }\n }\n)\n\nWrite-Debug \"Running tests to checking for reboot pending\"\nforeach ($test in $tests) {\n if (& $test) {\n Write-Debug \"Test found reboot pending: $test\"\n $IsPendingReboot = $true\n break\n }\n}\n \n\nif($IsPendingReboot -eq $true) {\n Write-Host \"Restarting\"\n Restart-Computer -Force\n}\nif($IsPendingReboot -eq $false) {\n Write-Host \"No Restart required\"\n}\n"
},
"Category": "Windows",
"HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/windows-restart-if-required.json",
"Website": "/step-templates/cb0ca23c-524d-44a9-86e0-2ae05989d6a0",
"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"
}
}
Page updated on Tuesday, January 14, 2020