【发布时间】:2017-12-01 05:20:39
【问题描述】:
我正在执行一个 C# exe,CRS.exe,我希望它返回一个非零值,例如 -1。我正在使用 ps 来取回价值,并在一个阶段内拥有它:
try{
pcode = (powershell(returnStdout: true, script: 'return Invoke-Expression -Command \" .\\perfmon\\CRS.exe hello \"'))''
echo "Pcode = ${pcode} "
}
catch (err) {echo err.message }
echo "Pcode = ${pcode} "
根据这篇文章,“通常情况下,以非零状态代码退出的脚本会导致步骤失败并出现异常。” -- Jenkins pipeline bubble up the shell exit code to fail the stage 我想处理这个非零结果,异常处理程序是唯一的方法吗? 以上运行结果:
Running PowerShell script
tester arg = hello
[Pipeline] echo
script returned exit code -1
[Pipeline] echo
Pcode = null
有趣的是,char 返回似乎没问题?这会返回而不抛出异常
icode = (powershell(returnStdout: true, script: 'return Invoke-Expression -Command \'.\\perfmon\\zipInstaller.ps1 -urlString ' + fileContents + "'"))
echo "icode = ${icode} "
Results in
[Pipeline] {
[Pipeline] powershell
[Chris] Running PowerShell script
[Pipeline] echo
icode = -5
我想从 exe 中获取返回码,并据此管理我的 groovy 管道流。任何见解将不胜感激。
【问题讨论】:
-
非零退出代码可能会导致命令在写入
pcode变量之前失败。如果您想对此进行测试,请尝试在 powershell 调用之前为 pcode 分配一个默认值。如果它为空,则说明您的命令有问题。如果它是默认值,则您的命令在写入变量之前失败。 -
另外,我认为命令末尾的
''是一个错字。