Why does a PowerShell script not end when there is a non-zero exit code using the call operator?

后端 未结 2 2125
忘了有多久
忘了有多久 2021-02-19 14:06

Why does a PowerShell script not end when there is a non-zero exit code using when using the call operator and $ErrorActionPerference = \"Stop\"?

Using the

2条回答
  •  面向向阳花
    2021-02-19 14:35

    The return code is not a PowerShell error - it's seen the same way as any other variable.

    You need to then act on the variable and throw an error using PowerShell for you script to see it as a terminating error:

    $ErrorActionPreference = "Stop"
    
    & cmd.exe /c "exit 1"
    
    if ($LASTEXITCODE -ne 0) { throw "Exit code is $LASTEXITCODE" }
    

提交回复
热议问题