如果&符命令失败,如何自动停止Powershell。示例:&$ cmd#现在失败

时间:2019-05-01 19:44:40

标签: powershell

我正在尝试编写一个Powershell工具控制脚本,该脚本使用&运算符调用其他“ .EXE”程序。示例:

$cmd = ".\buffalo_wing.exe"

& $cmd "--really_hot" "--extra_spicy"

# ok now my command has failed by returning non-zero value...
#  but powershell keeps on going like the energizer bunny..

我尝试设置:

$ErrorActionPreference="Stop"

但无济于事。

1 个答案:

答案 0 :(得分:0)

$cmd = ".\buffalo_wing.exe"

& $cmd "--really_hot" "-o wing.log"
if ($? -eq $false) {
    write-host -background DarkBlue -foreground Red "<Error Exit>"
    exit 1 
}