diff --git a/reboot_pending/reboot_pending.dsc.resource.json b/reboot_pending/reboot_pending.dsc.resource.json index be530a91d..590d9dc95 100644 --- a/reboot_pending/reboot_pending.dsc.resource.json +++ b/reboot_pending/reboot_pending.dsc.resource.json @@ -28,6 +28,13 @@ "rebootPending": { "type": "boolean", "readOnly": true + }, + "reasons": { + "type": [ + "array", + "null" + ], + "readOnly": true } } } diff --git a/reboot_pending/reboot_pending.resource.ps1 b/reboot_pending/reboot_pending.resource.ps1 index a64be69f8..baacd13ea 100644 --- a/reboot_pending/reboot_pending.resource.ps1 +++ b/reboot_pending/reboot_pending.resource.ps1 @@ -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 \ No newline at end of file +# 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 diff --git a/reboot_pending/tests/reboot_pending.tests.ps1 b/reboot_pending/tests/reboot_pending.tests.ps1 index 29a514f79..0fb257ca2 100644 --- a/reboot_pending/tests/reboot_pending.tests.ps1 +++ b/reboot_pending/tests/reboot_pending.tests.ps1 @@ -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 + } + } }