How to stop a PowerShell script on the first error?

后端 未结 8 1794
长情又很酷
长情又很酷 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:40

    You should be able to accomplish this by using the statement $ErrorActionPreference = "Stop" at the beginning of your scripts.

    The default setting of $ErrorActionPreference is Continue, which is why you are seeing your scripts keep going after errors occur.

提交回复
热议问题