Error Handling Non Zero Return Codes

#!/usr/bin/env pwsh

# Can make any non zero exit codes be treated as proper termination errors
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $true

function Main {
    param($arg1, $arg2)

    Write-Host "Starting script..."

    try {
        # Can invoke bash scripts,
        & $env:ISSH
        Write-Host "Command succeeded"
    }
    catch {
        # If bash scripts fail we can have a proper catch block
        Write-Host "CAUGHT ERROR: $($_.Exception.Message)"
    }

    Write-Host "Script completed."
}

# Invoke it at the end
Main -arg1 "value1" -arg2 "value2"

Backlinks