I found myself searching the internet again on how to use the Azure CLI from PowerShell so that I can use it in Azure Pipelines to create new Azure resources. The reason I want to do this with PowerShell is twofold:
I’ve fixed it before, but it took a while to find it again. That is why I am documenting it here, to save me some yak shaving in the future.
Running this Azure CLI command in PowerShell will result in an error, because storage accounts cannot have a dash or capitals in the name:
$StorageAccountName = "testRb-001"
$ResourceGroup = "myResourceGroup"
$location = "westeurope"
az storage account create -n $StorageAccountName -g $ResourceGroup -l $location --sku Standard_LRS
Result:
Seems like an error, what’s the issue then? Well, adding error handling like you’d expect from PowerShell will not work!
$StorageAccountName = "testRb-001"
$ResourceGroup = "myResourceGroup"
$location = "westeurope"
try {
az storage account create -n $StorageAccountName -g $ResourceGroup -l $location --sku Standard_LRS
Write-Host "Just continues"
}
catch {
Write-Host "An error occurred!"
}
You can see that PowerShell doesn’t notice the error and just continues:
Even adding -ErrorAction will not work.
The Azure CLI runs on JSON: it will try to give you JSON results on every call, so we can use that to see if we got any data back from the call. After converting the result, we can test to see if it has data:
$StorageAccountName = "testRb-001"
$ResourceGroup = "myResourceGroup"
$location = "westeurope"
$output = az storage account create -n $StorageAccountName -g $ResourceGroup -l $location --sku Standard_LRS | ConvertFrom-Json
if (!$output) {
Write-Error "Error creating storage account"
return
}
Do remember to wrap every call you need to run with this setup, and return to prevent PowerShell to continue with the next statement.
Writing the error to the output helps with:
There is a shorthand version of this code that you can use, if you don’t care about the information in the output (Thanks Rasťo !). You can use PowerShells about automatic variable: $?
to just check if the result was successful or not.
$StorageAccountName = "testRb-001"
$ResourceGroup = "myResourceGroup"
$location = "westeurope"
$output = az storage account create -n $StorageAccountName -g $ResourceGroup -l $location --sku Standard_LRS | ConvertFrom-Json
$LastExitCode
if (!$?) {
Write-Error "Error creating storage account"
return
}
As stated in the documentation:
Contains the execution status of the last operation. It contains TRUE if the last operation succeeded and FALSE if it failed.
I am just not sure yet as to why this works. I suspect $?
is looking at the $LastExitCode
, because this will print out 1
:
$StorageAccountName = "testRb-001"
$ResourceGroup = "myResourceGroup"
$location = "westeurope"
$output = az storage account create -n $StorageAccountName -g $ResourceGroup -l $location --sku Standard_LRS | ConvertFrom-Json
$LastExitCode
After posting this, I got asked why I am using the CLI to do this at all? Surely, Azure PowerShell or ARM Templates would be sufficient.
Here is why:
The reasons for not using bash is that:
Here is a shell example to make sure you are connected to the correct Azure Subscription, to be complete:
# Switch to the correct subscription
az account set --subscription ${SUBSCRIPTION_ID}
output=$(az account show | jq '.')
[[ -z "$output" ]] && printf "${FAILURE}Error using subscriptionId, halting execution${NEUTRAL}\n" && exit 1
subscriptionId=$(echo $output | jq -r '.id')