From 12a095e532f075f742b7663540e34b0375a405c5 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Sun, 8 Jun 2025 08:19:57 +0200 Subject: [PATCH 1/7] Add property for reasons on pendingreboot --- .../reboot_pending.dsc.resource.json | 4 ++ reboot_pending/reboot_pending.resource.ps1 | 37 ++++++++++++------- reboot_pending/tests/reboot_pending.tests.ps1 | 19 ++++++++++ 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/reboot_pending/reboot_pending.dsc.resource.json b/reboot_pending/reboot_pending.dsc.resource.json index 224a0e72b..ae985b6f1 100644 --- a/reboot_pending/reboot_pending.dsc.resource.json +++ b/reboot_pending/reboot_pending.dsc.resource.json @@ -21,6 +21,10 @@ "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..1f9b6119a 100644 --- a/reboot_pending/reboot_pending.resource.ps1 +++ b/reboot_pending/reboot_pending.resource.ps1 @@ -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 \ No newline at end of file +# 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 \ No newline at end of file diff --git a/reboot_pending/tests/reboot_pending.tests.ps1 b/reboot_pending/tests/reboot_pending.tests.ps1 index 29a514f79..ad8cf4041 100644 --- a/reboot_pending/tests/reboot_pending.tests.ps1 +++ b/reboot_pending/tests/reboot_pending.tests.ps1 @@ -14,4 +14,23 @@ 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 + # This is just an example, actual implementation may vary + 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 { + # Clean up the registry key to avoid affecting other tests + Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -ErrorAction SilentlyContinue + } + } } From b663a0582d7cfb1b05d011e0d4c6fd8da21cf427 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Sun, 8 Jun 2025 08:23:54 +0200 Subject: [PATCH 2/7] Format JSON --- reboot_pending/reboot_pending.dsc.resource.json | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/reboot_pending/reboot_pending.dsc.resource.json b/reboot_pending/reboot_pending.dsc.resource.json index b5d926506..590d9dc95 100644 --- a/reboot_pending/reboot_pending.dsc.resource.json +++ b/reboot_pending/reboot_pending.dsc.resource.json @@ -26,11 +26,14 @@ ], "properties": { "rebootPending": { - "type": "boolean", - "readOnly": true + "type": "boolean", + "readOnly": true }, "reasons": { - "type": ["array", "null"], + "type": [ + "array", + "null" + ], "readOnly": true } } From 767cd5ea46db00c93e6db0963de168be21c8feae Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Sun, 8 Jun 2025 08:26:08 +0200 Subject: [PATCH 3/7] Remove comment --- reboot_pending/tests/reboot_pending.tests.ps1 | 2 -- 1 file changed, 2 deletions(-) diff --git a/reboot_pending/tests/reboot_pending.tests.ps1 b/reboot_pending/tests/reboot_pending.tests.ps1 index ad8cf4041..0fb257ca2 100644 --- a/reboot_pending/tests/reboot_pending.tests.ps1 +++ b/reboot_pending/tests/reboot_pending.tests.ps1 @@ -18,7 +18,6 @@ Describe 'reboot_pending resource tests' { It 'reboot_pending should have a reason' -Skip:(!$IsWindows) { BeforeAll { # Ensure the system is in a state that requires a reboot - # This is just an example, actual implementation may vary 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 } @@ -29,7 +28,6 @@ Describe 'reboot_pending resource tests' { $out.actualState.reason | Should -Not -BeNullOrEmpty AfterAll { - # Clean up the registry key to avoid affecting other tests Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -ErrorAction SilentlyContinue } } From 2f19a055d2b11beefd003a3662ff07d728d883c0 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Wed, 11 Jun 2025 05:46:22 +0200 Subject: [PATCH 4/7] Resolve remarks --- reboot_pending/reboot_pending.resource.ps1 | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/reboot_pending/reboot_pending.resource.ps1 b/reboot_pending/reboot_pending.resource.ps1 index 1f9b6119a..dbc72c237 100644 --- a/reboot_pending/reboot_pending.resource.ps1 +++ b/reboot_pending/reboot_pending.resource.ps1 @@ -1,19 +1,22 @@ +# 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 = @() +$reasons = [System.Collections.Generic.List[string[]]]::new() if (Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending" -EA Ignore) { - $reasons += "Component Based Servicing" + $reasons.Add("Component Based Servicing") } if (Get-Item "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -EA Ignore) { - $reasons += "Windows Update" + $reasons.Add("Windows Update") } if (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager" -Name PendingFileRenameOperations -EA Ignore) { - $reasons += "Pending File Rename Operations" + $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 += "SCCM Client" + $reasons.Add("SCCM Client") } }catch{} @@ -21,4 +24,5 @@ $result = @{ rebootPending = $reasons.Count -gt 0 reason = if ($reasons.Count -gt 0) { $reasons } else { $null } } + return $result | ConvertTo-Json -Compress \ No newline at end of file From a80a32b6a385e04005291eb54b739866dabf63b5 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 11 Jun 2025 13:06:01 -0700 Subject: [PATCH 5/7] Update reboot_pending/reboot_pending.resource.ps1 --- reboot_pending/reboot_pending.resource.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reboot_pending/reboot_pending.resource.ps1 b/reboot_pending/reboot_pending.resource.ps1 index dbc72c237..baacd13ea 100644 --- a/reboot_pending/reboot_pending.resource.ps1 +++ b/reboot_pending/reboot_pending.resource.ps1 @@ -25,4 +25,4 @@ $result = @{ reason = if ($reasons.Count -gt 0) { $reasons } else { $null } } -return $result | ConvertTo-Json -Compress \ No newline at end of file +return $result | ConvertTo-Json -Compress From 4d84ad6891090259beded4d60c1ac5f8d71839f3 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Thu, 12 Jun 2025 18:42:04 +0200 Subject: [PATCH 6/7] Try/finally --- reboot_pending/tests/reboot_pending.tests.ps1 | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/reboot_pending/tests/reboot_pending.tests.ps1 b/reboot_pending/tests/reboot_pending.tests.ps1 index 0fb257ca2..3496e7a85 100644 --- a/reboot_pending/tests/reboot_pending.tests.ps1 +++ b/reboot_pending/tests/reboot_pending.tests.ps1 @@ -16,19 +16,18 @@ Describe 'reboot_pending resource tests' { } 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 + $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 - AfterAll { - Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -ErrorAction SilentlyContinue + $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 SilentlyContinue } } } From af51133ce37cf64aee85c3dc8c33c613fc17aab8 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 12 Jun 2025 10:37:50 -0700 Subject: [PATCH 7/7] Update reboot_pending/tests/reboot_pending.tests.ps1 --- reboot_pending/tests/reboot_pending.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reboot_pending/tests/reboot_pending.tests.ps1 b/reboot_pending/tests/reboot_pending.tests.ps1 index 3496e7a85..d6bf5a80c 100644 --- a/reboot_pending/tests/reboot_pending.tests.ps1 +++ b/reboot_pending/tests/reboot_pending.tests.ps1 @@ -27,7 +27,7 @@ Describe 'reboot_pending resource tests' { $LASTEXITCODE | Should -Be 0 $out.actualState.reason | Should -Not -BeNullOrEmpty } finally { - Remove-ItemProperty -Path $keyPath -Name $keyName -ErrorAction SilentlyContinue + Remove-ItemProperty -Path $keyPath -Name $keyName -ErrorAction Ignore } } }