Skip to content

Commit 4d84ad6

Browse files
committed
Try/finally
1 parent 4bab02b commit 4d84ad6

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

reboot_pending/tests/reboot_pending.tests.ps1

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@ Describe 'reboot_pending resource tests' {
1616
}
1717

1818
It 'reboot_pending should have a reason' -Skip:(!$IsWindows) {
19-
BeforeAll {
20-
# Ensure the system is in a state that requires a reboot
21-
if (-not (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired" -ErrorAction SilentlyContinue)) {
22-
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -Value 1 -PropertyType DWord -Force | Out-Null
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
2324
}
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
2925

30-
AfterAll {
31-
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" -Name "RebootRequired" -ErrorAction SilentlyContinue
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 SilentlyContinue
3231
}
3332
}
3433
}

0 commit comments

Comments
 (0)