我有一个脚本,其中包括:
try {
Compress-Archive -Path "$($folder.FullName)\*" -CompressionLevel Optimal -DestinationPath $FullPath -Force
} catch {
Write-Output "`nFailed to create zip"
}
-Path文件夹中的某些文件由另一个用户打开,因此Compress-Archive无法将它们添加到zip中。它向stderr报告此失败,但它不会抛出错误。
有没有什么办法可以确定命令只是部分成功而没有解析stderr或重新打开zip并比较内容?
这听起来像Compress-Archive
抛出一个非终止错误,你无法用try
/ catch
捕获。
但是,如果将-ErrorAction Stop
添加到Compress-Archive
调用中,则非终止错误将被提升为脚本终止错误,这将触发您的catch
处理程序。
有关PowerShell复杂错误处理规则的概述,请参阅this GitHub issue。