Skip to content

Add pending reboot reason #873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions reboot_pending/reboot_pending.dsc.resource.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
"rebootPending": {
"type": "boolean",
"readOnly": true
},
"reasons": {
"type": [
"array",
"null"
],
"readOnly": true
}
}
}
Expand Down
37 changes: 24 additions & 13 deletions reboot_pending/reboot_pending.resource.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
# Reg keys are documented here: https://learn.microsoft.com/en-us/mem/configmgr/core/servers/deploy/install/list-of-prerequisite-checks#pending-system-restart
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { return @{ rebootPending = $true } | ConvertTo-Json -Compress }
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { return @{ rebootPending = $true } | ConvertTo-Json -Compress }
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return @{ rebootPending = $true } | ConvertTo-Json -Compress }
try {
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
$status = $util.DetermineIfRebootPending()
if(($status -ne $null) -and $status.RebootPending){
return @{ rebootPending = $true } | ConvertTo-Json -Compress
}
}catch{}

return @{ rebootPending = $false } | ConvertTo-Json -Compress
# Reg keys are documented here: https://learn.microsoft.com/en-us/mem/configmgr/core/servers/deploy/install/list-of-prerequisite-checks#pending-system-restart
$reasons = @()
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) {
$reasons += "Component Based Servicing"
}
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) {
$reasons += "Windows Update"
}
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) {
$reasons += "Pending File Rename Operations"
}
try {
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
$status = $util.DetermineIfRebootPending()
if(($null -ne $status) -and $status.RebootPending){
$reasons += "SCCM Client"
}
}catch{}

$result = @{
rebootPending = $reasons.Count -gt 0
reason = if ($reasons.Count -gt 0) { $reasons } else { $null }
}
return $result | ConvertTo-Json -Compress
17 changes: 17 additions & 0 deletions reboot_pending/tests/reboot_pending.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,21 @@ Describe 'reboot_pending resource tests' {
$LASTEXITCODE | Should -Be 0
$out.results.result.actualState.rebootPending | Should -Not -BeNullOrEmpty
}

It 'reboot_pending should have a reason' -Skip:(!$IsWindows) {
BeforeAll {
# Ensure the system is in a state that requires a reboot
if (-not (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction SilentlyContinue)) {
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -Value 1 -PropertyType DWord -Force | Out-Null
}
}

$out = dsc resource get -r Microsoft.Windows/RebootPending | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.actualState.reason | Should -Not -BeNullOrEmpty

AfterAll {
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -ErrorAction SilentlyContinue
}
}
}
Loading