这个自我回答的问题试图解决在 PowerShell中处理进程退出代码的两个不同方面:
在 PowerShell 代码中,如何查询外部进程设置的退出代码(对外部程序的调用),这些退出代码如何与 PowerShell 的错误处理相结合?
当其他人通过其 CLI pwsh(PowerShell Core)/ powershell.exe(Windows PowerShell)调用 PowerShell 时,什么决定了 PowerShell 进程的退出代码,该代码将成功与失败传达给调用进程(可能是构建/CI/自动化服务器任务) ,计划任务或不同的外壳,例如)。
我使用Git来部署我的Web应用程序.所以在Teamcity我准备我的应用程序(编译,缩小JS和HTML,删除未使用的文件等等)然后我有一个Powershell构建步骤:
$ErrorActionPreference = "Stop"
git init
git remote add origin '%env.gitFolder%'
git fetch
git reset --mixed origin/master
git add .
git commit -m '%build.number%'
git push origin master
Run Code Online (Sandbox Code Playgroud)
但是如果抛出异常,脚本会继续(即使我设置$ErrorActionPreference = "Stop")并且构建成功.
我希望脚本在出现错误时停止,并且构建失败.
我试图进行Format stderr output as: error构建步骤Fail build if: an error message is logged by build runner,因此构建失败,但脚本继续,因此它创建了一个愚蠢的提交.
我尝试在我的脚本中添加一个try-catch,但它没有进入catch ...
有没有人有想法停止脚本并失败构建错误?
抱歉我的英语,我是法国人...... ^^