Skip to content

Commit 12a095e

Browse files
committed
Add property for reasons on pendingreboot
1 parent 07e26f9 commit 12a095e

File tree

3 files changed

+47
-13
lines changed

3 files changed

+47
-13
lines changed

reboot_pending/reboot_pending.dsc.resource.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
"rebootPending": {
2222
"type": "boolean",
2323
"readOnly": true
24+
},
25+
"reasons": {
26+
"type": ["array", "null"],
27+
"readOnly": true
2428
}
2529
}
2630
}
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
# Reg keys are documented here: https://learn.microsoft.com/en-us/mem/configmgr/core/servers/deploy/install/list-of-prerequisite-checks#pending-system-restart
2-
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { return @{ rebootPending = $true } | ConvertTo-Json -Compress }
3-
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { return @{ rebootPending = $true } | ConvertTo-Json -Compress }
4-
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { return @{ rebootPending = $true } | ConvertTo-Json -Compress }
5-
try {
6-
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
7-
$status = $util.DetermineIfRebootPending()
8-
if(($status -ne $null) -and $status.RebootPending){
9-
return @{ rebootPending = $true } | ConvertTo-Json -Compress
10-
}
11-
}catch{}
12-
13-
return @{ rebootPending = $false } | ConvertTo-Json -Compress
1+
# Reg keys are documented here: https://learn.microsoft.com/en-us/mem/configmgr/core/servers/deploy/install/list-of-prerequisite-checks#pending-system-restart
2+
$reasons = @()
3+
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) {
4+
$reasons += "Component Based Servicing"
5+
}
6+
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) {
7+
$reasons += "Windows Update"
8+
}
9+
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) {
10+
$reasons += "Pending File Rename Operations"
11+
}
12+
try {
13+
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
14+
$status = $util.DetermineIfRebootPending()
15+
if(($null -ne $status) -and $status.RebootPending){
16+
$reasons += "SCCM Client"
17+
}
18+
}catch{}
19+
20+
$result = @{
21+
rebootPending = $reasons.Count -gt 0
22+
reason = if ($reasons.Count -gt 0) { $reasons } else { $null }
23+
}
24+
return $result | ConvertTo-Json -Compress

reboot_pending/tests/reboot_pending.tests.ps1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,23 @@ Describe 'reboot_pending resource tests' {
1414
$LASTEXITCODE | Should -Be 0
1515
$out.results.result.actualState.rebootPending | Should -Not -BeNullOrEmpty
1616
}
17+
18+
It 'reboot_pending should have a reason' -Skip:(!$IsWindows) {
19+
BeforeAll {
20+
# Ensure the system is in a state that requires a reboot
21+
# This is just an example, actual implementation may vary
22+
if (-not (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction SilentlyContinue)) {
23+
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -Value 1 -PropertyType DWord -Force | Out-Null
24+
}
25+
}
26+
27+
$out = dsc resource get -r Microsoft.Windows/RebootPending | ConvertFrom-Json
28+
$LASTEXITCODE | Should -Be 0
29+
$out.actualState.reason | Should -Not -BeNullOrEmpty
30+
31+
AfterAll {
32+
# Clean up the registry key to avoid affecting other tests
33+
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -ErrorAction SilentlyContinue
34+
}
35+
}
1736
}

0 commit comments

Comments
 (0)