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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With