Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell script Compress-Archive partial failure

Tags:

powershell

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?

like image 819
The Trav Avatar asked Jul 20 '26 23:07

The Trav


1 Answers

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.

like image 66
mklement0 Avatar answered Jul 23 '26 22:07

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!