Reputation: 2005
I have a script that includes:
try {
Compress-Archive -Path "$($folder.FullName)\*" -CompressionLevel Optimal -DestinationPath $FullPath -Force
} catch {
Write-Output "`nFailed to create zip"
}
Some of the files in the -Path folder are opened by another user, so Compress-Archive is unable to add them to the zip. It reports this failure to stderr, however it doesn't throw an error.
Is there any way I can determine that the command has only partially succeeded without parsing stderr or re-opening the zip and comparing contents?
Upvotes: 3
Views: 3254
Reputation: 438143
It sounds like Compress-Archive
is throwing a non-terminating error, which you cannot catch with try
/ catch
.
However, if you add -ErrorAction Stop
to the Compress-Archive
call, the non-terminating error will be promoted to a script-terminating error, which will trigger your catch
handler.
For an overview of PowerShell's complex error-handling rules, see this GitHub issue.
Upvotes: 3