I have read up the info on the SO link here on how to handle errors when using powershell as a wrapper scrip to deploy bicep deployments. Below is my experience so far.
I implemented the below.
try {
az deployment group create --resource-group $rg --template-file $TemplateFile --parameters $ParameterFile --name $deployment_name | ConvertFrom-Json
if($LASTEXITCODE){
Write-Host "ERROR: in Az Group"
Throw "ERROR: in Az Group"
}
# Error in az group
# More az cli commands
}
catch {
Write-Host "ERROR: $Error"
}
In my case, I had the error below.
ERROR: {"status":"Failed","error":{"code":"DeploymentFailed","target":"/subscriptions/xx/resourceGroups/xx/providers/Microsoft.Resources/deployments/xx","message":"At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-deployment-operations for usage details.","details":[{"code":"InvalidTemplate","message":"Deployment template parse failed: 'Error reading string. Unexpected token: StartObject. Path 'tags'.'."}]}}
ERROR: in Az Group deployment
There has been an error !. ERROR: in Az Group deployment for the deployment xx The module 'cdaz-delta-rebuild' could not be loaded. For more information, run 'Import-Module cdaz-delta-rebuild'.
when we examine the error again, there are 2 elements to it. The first is Unexpected token: StartObject. Path 'tags'.'."}]}}
The actual issue is the fact that my parameter was expecting a string, I passed an object to it, resolved that and problem gone. what worries me is that the second part of the error is somewhat confusing ERROR: in Az Group deployment for the deployment xx The module 'cdaz-delta-rebuild' could not be loaded. For more information, run 'Import-Module cdaz-delta-rebuild'
I have no idea what this is, other than the fact that the folder where my bicep code is stored is called delta-rebuild and perhaps maybe I ran a cd command on the terminal.
My questions are as follows.
What is the right way to trap errors when using PowerShell to call az deployment ...
commands and lastly, errors come up with a white font on a black background, I would like it to be in red font colour so that it doesn’t get mixed up with bicep warning messages.