Skip to content

Commit 3beab6e

Browse files
committed
Change PSScript resource to not exit on non-terminating errors
1 parent 6c162ed commit 3beab6e

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

resources/PSScript/psscript.ps1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,16 @@ try {
138138
$asyncResult = $ps.BeginInvoke()
139139
while (-not $asyncResult.IsCompleted) {
140140
Write-TraceQueue
141-
141+
142142
Start-Sleep -Milliseconds 100
143143
}
144144
$outputCollection = $ps.EndInvoke($asyncResult)
145145
Write-TraceQueue
146146

147147

148148
if ($ps.HadErrors) {
149-
# If there are any errors, we will exit with an error code
150-
Write-DscTrace -Now -Level Error -Message 'Errors occurred during script execution.'
151-
exit 1
149+
# Errors can be non-terminating, so we just write a warning and continue
150+
Write-DscTrace -Now -Level Warn -Message 'Non-terminating errors occurred during script execution.'
152151
}
153152

154153
foreach ($output in $outputCollection) {

resources/PSScript/psscript.tests.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,18 @@ Describe 'Tests for PSScript resource' {
324324
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*INFO*:*This is a host message*'
325325
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*INFO*:*This is a verbose message*'
326326
}
327+
328+
It 'Non-terminating errors do not cause script to fail' {
329+
$yaml = @'
330+
getScript: |
331+
$ErrorActionPreference = 'Continue'
332+
Write-Error "This is an error"
333+
"This should still be output"
334+
'@
335+
$result = dsc resource get -r 'Microsoft.DSC.Transitional/PowerShellScript' -i $yaml 2> $TestDrive/error.txt | ConvertFrom-Json
336+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.txt -Raw | Out-String)
337+
$result.actualState.output.Count | Should -Be 1 -Because ($result | ConvertTo-Json | Out-String)
338+
$result.actualState.output[0] | Should -BeExactly "This should still be output"
339+
(Get-Content $TestDrive/error.txt -Raw) | Should -BeLike '*WARN*:*Non-terminating errors occurred during script execution.*'
340+
}
327341
}

0 commit comments

Comments
 (0)