I have installed the azure cli on my local system and I am able to run the azure cli commands in Windows Powershell. When I run any command which is not correct or throwing any exception, I am able to see it on the console. But how can I capture this exception using Try...Catch . I want to handle the exception using try..catch in powershell script.
Please help me on this.
code snippet:
Suppose my ClientSecret is wrong then the below command will through an exception, how can I capture this in Catch block??
Try
{
$TenantId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$ClientId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$ClientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
az login --service-principal -u $ClientId -p $ClientSecret --tenant $TenantId
}
Catch
{
$ErrorMessage = $_.Exception.Message
Write-Output $ErrorMessage
}