Skip to content

Commit 56910ef

Browse files
authored
Merge pull request #873 from Gijsreyn/add-pending-reboot-reason
Add pending reboot reason
2 parents 202aed5 + af51133 commit 56910ef

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

reboot_pending/reboot_pending.dsc.resource.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@
2828
"rebootPending": {
2929
"type": "boolean",
3030
"readOnly": true
31+
},
32+
"reasons": {
33+
"type": [
34+
"array",
35+
"null"
36+
],
37+
"readOnly": true
3138
}
3239
}
3340
}
Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
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+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
# Reg keys are documented here: https://learn.microsoft.com/en-us/mem/configmgr/core/servers/deploy/install/list-of-prerequisite-checks#pending-system-restart
5+
$reasons = [System.Collections.Generic.List[string[]]]::new()
6+
if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) {
7+
$reasons.Add("Component Based Servicing")
8+
}
9+
if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) {
10+
$reasons.Add("Windows Update")
11+
}
12+
if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) {
13+
$reasons.Add("Pending File Rename Operations")
14+
}
15+
try {
16+
$util = [wmiclass]"\\.\root\ccm\clientsdk:CCM_ClientUtilities"
17+
$status = $util.DetermineIfRebootPending()
18+
if(($null -ne $status) -and $status.RebootPending){
19+
$reasons.Add("SCCM Client")
20+
}
21+
}catch{}
22+
23+
$result = @{
24+
rebootPending = $reasons.Count -gt 0
25+
reason = if ($reasons.Count -gt 0) { $reasons } else { $null }
26+
}
27+
28+
return $result | ConvertTo-Json -Compress

reboot_pending/tests/reboot_pending.tests.ps1

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,20 @@ 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+
$keyPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update"
20+
$keyName = "RebootRequired"
21+
try {
22+
if (-not (Get-ItemProperty "$keyPath\$keyName" -ErrorAction SilentlyContinue)) {
23+
New-ItemProperty -Path $keyPath -Name $keyName -Value 1 -PropertyType DWord -Force | Out-Null
24+
}
25+
26+
$out | dsc resource get -r Microsoft.Windows/RebootPending | ConvertFrom-Json
27+
$LASTEXITCODE | Should -Be 0
28+
$out.actualState.reason | Should -Not -BeNullOrEmpty
29+
} finally {
30+
Remove-ItemProperty -Path $keyPath -Name $keyName -ErrorAction Ignore
31+
}
32+
}
1733
}

0 commit comments

Comments
 (0)