I’ve previously written a blog post about how to handle Azure CLI errors in PowerShell. The general pattern involves redirecting any error streams to the correct place, and checking for a known exit code. There’s an exciting new feature in PowerShell from version 7.3 onwards that makes capturing errors for native applications like the Azure CLI much simpler. Enter: $PSNativeCommandErrorActionPreference
PSNativeCommandErrorActionPreference is a preference variable that when set to true
, allows for native commands to be handled in a more ‘PowerShell’ way.
To enable it, you just need to set $PSNativeCommandErrorActionPreference = $true
somewhere in your script, and then you can use a conventional try/catch for your error handling. Check out this before/after example:
Before
- Assign variable to output
- Redirect the error stream and assign to variable
$err
to ensure any error(s) get captured - If the
$LASTEXITCODE
isn’t a success, throw the contents of the$err
variable
$err = $($podList = kubectl get pods) 2>&1
if ($LASTEXITCODE -ne 0) {
throw $err
}
After
- Set the
$PSNativeCommandUseErrorActionPreference
totrue
- Set
$ErrorActionPreference
toStop
- Handle the error in a traditional try/catch and it’ll throw if there’s a non-zero exit code (Note you may still want to redirect the error stream depending on the command)
$PSNativeCommandUseErrorActionPreference = $true
$ErrorActionPreference = 'Stop'
try {
$podList = kubectl get pods
} catch {
throw
}