How to stop a PowerShell script on the first error?

后端 未结 8 1771
长情又很酷
长情又很酷 2020-12-02 06:11

I want my PowerShell script to stop when any of the commands I run fail (like set -e in bash). I\'m using both Powershell commands (New-Object System.Net.

8条回答
  •  温柔的废话
    2020-12-02 06:42

    I'm new to powershell but this seems to be most effective:

    doSomething -arg myArg
    if (-not $?) {throw "Failed to doSomething"}
    

提交回复
热议问题