我一直不知道如何让它发挥作用。
我希望它像提取zip一样提取zip,但如果失败(错误图片(,我希望它删除zip并再次卷曲它。
它给出错误的原因是zip损坏,这是因为用户在最初的zip安装过程中关闭了程序。
if EXIST "%UserProfile%Downloads100 Player Among US.zip" (
echo "---Zip Detected, Extracting it now---"
powershell -Command "Expand-Archive -Force '%UserProfile%Downloads100 Player Among US.zip' '%UserProfile%Downloads'"
if There is an error (
DEL "%UserProfile%Downloads100 Player Among US.zip"
echo "---Corrpupted Zip, I'm installing it again---"
curl "link"
)
)
若要处理批处理脚本中的Powershell错误,必须在出现错误时从Powershell返回非零退出代码。
Powershell在以下情况下返回非零退出代码:
- 脚本使用
exit N
语句终止,其中N指定非零退出代码 - 终止错误没有被捕获,所以它";叶子";脚本
- 脚本中的语法错误,例如无效命令
默认情况下,当提取失败时,Expand-Archive
会导致非终止错误。我们可以通过传递公共参数-ErrorAction Stop
或在调用命令之前设置首选变量$ErrorActionPreference = 'Stop'
,将其转化为终止错误。
使用-ErrorAction
参数的示例:
powershell -Command "Expand-Archive -ErrorAction Stop -Force '%UserProfile%Downloads100 Player Among US.zip' '%UserProfile%Downloads'"
if ERRORLEVEL 1 (
:: Handle the error
)
使用$ErrorActionPreference
:的示例
powershell -Command "$ErrorActionPreference='Stop'; Expand-Archive -Force '%UserProfile%Downloads100 Player Among US.zip' '%UserProfile%Downloads'"
if ERRORLEVEL 1 (
:: Handle the error
)
设置$ErrorActionPreference
变量可以简化运行多个命令的脚本。