Andres Riofrio
Andres Riofrio

Reputation: 10377

How to stop a PowerShell script on the first error?

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.WebClient) and programs (.\setup.exe).

Upvotes: 425

Views: 290744

Answers (13)

goric
goric

Reputation: 11905

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.


Before powershell 7.4.0, this only affected cmdlets and not native programs that set a non-zero exit code.

But now in 7.4, the PSNativeCommandErrorActionPreference experimental feature is enabled by default, so when $PSNativeCommandUseErrorActionPreference = $true then $ErrorActionPreference = "Stop" will stop execution even for native programs.

Now the script:

$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$PSNativeCommandUseErrorActionPreference = $true # might be true by default

cmd /c "exit 1"
"something else"

Will fail with

NativeCommandExitException: C:\test.ps1:4:1
Line |
   4 |  cmd /c "exit 1"
     |  ~~~~~~~~~~~~~~~
     | Program "cmd.exe" ended with non-zero exit code: 1.

and won't execute "something else".

Upvotes: 114

masterxilo
masterxilo

Reputation: 2788

I always use a helper function like this to invoke any "classic" binaries (.exe) that don't throw errors/exceptions that powershell understands:

function _(
    [Parameter(Mandatory=$True)][string]$exe, 
    [Parameter(Mandatory=$True, ValueFromRemainingArguments=$True)][string[]]$arguments
) {
    &$exe @arguments
    if ($LASTEXITCODE -ne 0) { throw "ERROR running '$exe $(($arguments) -join ' ')' failed with LASTEXITCODE=$LASTEXITCODE" }
}

# usage:
_ python --version

Upvotes: 2

Damian
Damian

Reputation: 559

I recommend to apply simple exit code validation. Depends on how sophisticated solution you need.

Best for single validation - stops only when docker-compose fails:

docker-compose up --detach || exit 1

Good if you want to stop the script and propagate error message

docker-compose up --detach || throw "Script has failed, check logs above"

or more complex and reusable function:

function terminate() {
    Write-Error "Found exit code: $LASTEXITCODE"
    throw "Last executed operation has failed, check logs above!"
}

docker-compose up --detach || terminate

Upvotes: -1

Nasreddine Galfout
Nasreddine Galfout

Reputation: 2591

for people coming here on 2021 this is my solution that covers both cmdlets and programs

function CheckLastExitCode {
    param ([int[]]$SuccessCodes = @(0))

    if (!$?) {
        Write-Host "Last CMD failed $LastExitCode" -ForegroundColor Red
        #GoToWrapperDirectory in my code I go back to the original directory that launched the script
        exit
    }

    if ($SuccessCodes -notcontains $LastExitCode) {
        Write-Host "EXE RETURNED EXIT CODE $LastExitCode" -ForegroundColor Red
        #GoToWrapperDirectory in my code I go back to the original directory that launched the script
        exit
    } 
    
}

you can use it like this

cd NonExistingpath
CheckLastExitCode

Upvotes: 7

nicolas-van
nicolas-van

Reputation: 1051

As far as I know, Powershell does not have any automatic handling of non-zero exit codes returned by sub-programs it invokes.

The only solution I know about so far to mimick the behavior of bash -e is to add this check after every call to an external command:

if(!$?) { Exit $LASTEXITCODE }

Upvotes: 5

Jarekczek
Jarekczek

Reputation: 7896

Seems like simple rethrow does the trick.

param ([string] $Path, [string] $Find, [string] $Replace)
try {
  ((Get-Content -path $Path -Raw) -replace $Find, $Replace) | Set-Content -Path $Path
  Write-Output Completed.
} catch {
  # Without try/catch block errors don't interrupt program flow.
  throw
}

Now output Completed appears only after successful execution.

Upvotes: 2

aggieNick02
aggieNick02

Reputation: 2787

Sadly, due to buggy cmdlets like New-RegKey and Clear-Disk, none of these answers are enough. I've currently settled on the following code in a file called ps_support.ps1:

Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$PSDefaultParameterValues['*:ErrorAction']='Stop'
function ThrowOnNativeFailure {
    if (-not $?)
    {
        throw 'Native Failure'
    }
}

Then in any powershell file, after the CmdletBinding and Param for the file (if present), I have the following:

$ErrorActionPreference = "Stop"
. "$PSScriptRoot\ps_support.ps1"

The duplicated ErrorActionPreference = "Stop" line is intentional. If I've goofed and somehow gotten the path to ps_support.ps1 wrong, that needs to not silently fail!

I keep ps_support.ps1 in a common location for my repo/workspace, so the path to it for the dot-sourcing may change depending on where the current .ps1 file is.

Any native call gets this treatment:

native_call.exe
ThrowOnNativeFailure

Having that file to dot-source has helped me maintain my sanity while writing powershell scripts. :-)

Upvotes: 60

ubi
ubi

Reputation: 4399

Redirecting stderr to stdout seems to also do the trick without any other commands/scriptblock wrappers although I can't find an explanation why it works that way..

# test.ps1

$ErrorActionPreference = "Stop"

aws s3 ls s3://xxx
echo "==> pass"

aws s3 ls s3://xxx 2>&1
echo "shouldn't be here"

This will output the following as expected (the command aws s3 ... returns $LASTEXITCODE = 255)

PS> .\test.ps1

An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied
==> pass

Upvotes: 1

Peter L
Peter L

Reputation: 3361

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

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

Upvotes: 17

Lucas
Lucas

Reputation: 14969

A slight modification to the answer from @alastairtree:

function Invoke-Call {
    param (
        [scriptblock]$ScriptBlock,
        [string]$ErrorAction = $ErrorActionPreference
    )
    & @ScriptBlock
    if (($lastexitcode -ne 0) -and $ErrorAction -eq "Stop") {
        exit $lastexitcode
    }
}

Invoke-Call -ScriptBlock { dotnet build . } -ErrorAction Stop

The key differences here are:

  1. it uses the Verb-Noun (mimicing Invoke-Command)
  2. implies that it uses the call operator under the covers
  3. mimics -ErrorAction behavior from built in cmdlets
  4. exits with same exit code rather than throwing exception with new message

Upvotes: 22

alastairtree
alastairtree

Reputation: 4289

You need slightly different error handling for powershell functions and for calling exe's, and you need to be sure to tell the caller of your script that it has failed. Building on top of Exec from the library Psake, a script that has the structure below will stop on all errors, and is usable as a base template for most scripts.

Set-StrictMode -Version latest
$ErrorActionPreference = "Stop"


# Taken from psake https://github.com/psake/psake
<#
.SYNOPSIS
  This is a helper function that runs a scriptblock and checks the PS variable $lastexitcode
  to see if an error occcured. If an error is detected then an exception is thrown.
  This function allows you to run command-line programs without having to
  explicitly check the $lastexitcode variable.
.EXAMPLE
  exec { svn info $repository_trunk } "Error executing SVN. Please verify SVN command-line client is installed"
#>
function Exec
{
    [CmdletBinding()]
    param(
        [Parameter(Position=0,Mandatory=1)][scriptblock]$cmd,
        [Parameter(Position=1,Mandatory=0)][string]$errorMessage = ("Error executing command {0}" -f $cmd)
    )
    & $cmd
    if ($lastexitcode -ne 0) {
        throw ("Exec: " + $errorMessage)
    }
}

Try {

    # Put all your stuff inside here!

    # powershell functions called as normal and try..catch reports errors 
    New-Object System.Net.WebClient

    # call exe's and check their exit code using Exec
    Exec { setup.exe }

} Catch {
    # tell the caller it has all gone wrong
    $host.SetShouldExit(-1)
    throw
}

Upvotes: 17

harvey263
harvey263

Reputation: 76

I came here looking for the same thing. $ErrorActionPreference="Stop" kills my shell immediately when I'd rather see the error message (pause) before it terminates. Falling back on my batch sensibilities:

IF %ERRORLEVEL% NEQ 0 pause & GOTO EOF

I found that this works pretty much the same for my particular ps1 script:

Import-PSSession $Session
If ($? -ne "True") {Pause; Exit}

Upvotes: 2

Keith Hill
Keith Hill

Reputation: 202032

$ErrorActionPreference = "Stop" will get you part of the way there (i.e. this works great for cmdlets).

However for EXEs you're going to need to check $LastExitCode yourself after every exe invocation and determine whether that failed or not. Unfortunately I don't think PowerShell can help here because on Windows, EXEs aren't terribly consistent on what constitutes a "success" or "failure" exit code. Most follow the UNIX standard of 0 indicating success but not all do. Check out the CheckLastExitCode function in this blog post. You might find it useful.

Upvotes: 493

Related Questions