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

Merged
merged 11 commits into from
Jun 12, 2025
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
41 changes: 28 additions & 13 deletions reboot_pending/reboot_pending.resource.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
# 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# 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 = [System.Collections.Generic.List[string[]]]::new()
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) {
$reasons.Add("Component Based Servicing")
}
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) {
$reasons.Add("Windows Update")
}
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) {
$reasons.Add("Pending File Rename Operations")
}
try {
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
$status = $util.DetermineIfRebootPending()
if(($null -ne $status) -and $status.RebootPending){
$reasons.Add("SCCM Client")
}
}catch{}

$result = @{
rebootPending = $reasons.Count -gt 0
reason = if ($reasons.Count -gt 0) { $reasons } else { $null }
}

return $result | ConvertTo-Json -Compress
16 changes: 16 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,20 @@ 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) {
$keyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
$keyName = "RebootRequired"
try {
if (-not (Get-ItemProperty "$keyPath\$keyName" -ErrorAction SilentlyContinue)) {
New-ItemProperty -Path $keyPath -Name $keyName -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
} finally {
Remove-ItemProperty -Path $keyPath -Name $keyName -ErrorAction Ignore
}
}
}
Loading