如果使用 -File 参数调用,则当发生错误时,Powershell 将返回 0 退出代码。这意味着我的构建是绿色的,但它不应该是:(
例如:
(在wtf.ps1中)
$ErrorActionPreference = "Stop";
$null.split()
(cmd)
powershell -file c:\wtf.ps1
You cannot call a method on a null-valued expression.
At C:\wtf.ps1:3 char:12
+ $null.split <<<< ()
+ CategoryInfo : InvalidOperation: (split:String) [], ParentConta
insErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull
echo %errorlevel%
0
powershell c:\wtf.ps1
You cannot call a method on a null-valued expression.
At C:\wtf.ps1:3 char:12
+ $null.split <<<< ()
+ CategoryInfo : InvalidOperation: (split:String) [], ParentConta
insErrorRecordException
+ FullyQualifiedErrorId : InvokeMethodOnNull
echo %errorlevel%
1
有任何想法吗?
(我几乎尝试了前两页中的所有想法:https://www.google.co.uk/search?q=powershell+file+argument+exit+code https://www.google.co.uk/search?q=powershell+file+argument+exit+code已经)
在脚本中,使用 exit 关键字和您选择的数字:
exit 34
这是我用来测试这个的脚本:
## D:\Scripts\Temp\exit.ps1 ##
try{
$null.split()
}
catch
{
exit 34
}
exit 2
#############################
# launch powershell from cmd
C:\> powershell -noprofile -file D:\Scripts\Temp\exit.ps1
C:\>echo %errorlevel%
34
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)