Powershell脚本Compress-Archive部分失败

问题描述 投票:1回答:1

我有一个脚本,其中包括:

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并比较内容?

powershell
1个回答
0
投票

这听起来像Compress-Archive抛出一个非终止错误,你无法用try / catch捕获。

但是,如果将-ErrorAction Stop添加到Compress-Archive调用中,则非终止错误将被提升为脚本终止错误,这将触发您的catch处理程序。

有关PowerShell复杂错误处理规则的概述,请参阅this GitHub issue

© www.soinside.com 2019 - 2024. All rights reserved.