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

后端 未结 2 604
北荒
北荒 2021-02-19 14:13

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:30

    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" }
    

提交回复
热议问题