DevOps | Scripts | Automation

Azure CLIPowershell

How to handle errors in Azure CLI?

Overview

Azure CLI can run on multiple platforms/terminals, so each platform has its own methods for dealing with errors for different tools. In the case of Azure CLI, it doesn’t support its own native way of dealing with errors. We need to use a Terminal (PowerShell, CMD, bash) based approach to handle this.

Example

Let’s take the example of setting up an Azure subscription. We are setting up a subscription that is not available or you are not authorized to access.

az account set -s notavailable

Command Prompt / PowerShell error,

Not let’s check its official website for this command if any error-handling mechanism is provided.

If you see in the global parameters list, there is only one parameter related to error (–only-show-errors) and that is also not helpful in dealing with it.

$ErrorActionPreference variable in PowerShell

As most people use PowerShell to run Azure CLI, let’s handle AZ CLI error with PowerShell way. To deal with the error handling in PowerShell $ErrorActionPreference automatic variable we can use.

By default, its value is Continue, which means it will continue executing scripts even if the command throws an error. For example, We are setting Azure subscription which is not available.

az account set -s notavailable
Write-Host "Authorization Passed"

Ideally, the second line should not run but as the PowerShell uses the Continue value for the $ErrorActionPreference variable, the second line will execute.

To handle this, we can use the question mark variable ($?), which checks if the previous command failed or succeeded. So the code would be,

az account set -s notavailable

if($?) {
    Write-Host "Authorization Passed" -f Green
}
else {
    Write-Host "Authorization failed" -f Red
}

We are now getting the proper output. However, the error is still displayed from the main command. We need to suppress that alert.

If that error was from PowerShell native command then we could have used “-ErrorAction Ignore” parameter to suppress the alert but in this case, we can’t even set Ignore value at ErrorActionPreference because that is not allowed in the PowerShell.

We need to use Try/Catch block instead. To use this block, we need to terminate the error by setting the ErrorActionPeference value to Stop so that PowerShell can handle the exception. For example,

$ErrorActionPreference = "Stop"

try {
    az account set -s notavailable
    Write-Host "Authorization Passed" -f Green
}

catch {
 Write-Host "Authorization failed" -f Red
}

However, you can print the exception that is generated from the command instead of the custom message. For example,

$ErrorActionPreference = "Stop"

try {
    az account set -s notavailable
    Write-Host "Authorization Passed" -f Green
}

catch {
 Write-Host "$($_.Exception.Message)" -f Red
}

Use Multiple error handling

Now what about the situation when we sometimes need a script to continue execution even if there is an error from the command? In that case, use the function and define ErrorActionPreference for each function.

So ErrorActionPreference will have scope until that function only. For example,


function Test1 {
    $ErrorActionPreference = "Stop"

    try {
        az account set -s notavailable
        Write-Host "Authorization Passed" -f Green
    }

    catch {
     Write-Host "Authorization failed" -f Red
    }

}

function Test2 {
    $ErrorActionPreference = "SilentlyContinue"

    az account set -s test123
    Write-Host "THis will run"
}

# Run Function-1
Test1


# Run Function-2
Test2
Error Handling

That’s magic. Now you know how you can handle errors for PowerShell native and non-PowerShell commands.

Loading