Added try catch block added for azlogin
This PR adds a try-catch block for the azlogin function. * If an account is already logged in, it prints a warning message "Already logged in...". * If the login fails, it prints an error message "Login failed...". This change will help to handle the login process more gracefully and provide better user feedback.
Related issue:
https://github.com/actions/runner-images/issues/10236
Check list
- [x] Related issue / work item is attached
- [ ] Tests are written (if applicable)
- [ ] Documentation is updated (if applicable)
- [ ] Changes are tested and related VM images are successfully generated
I just wanted to add here that the current approach here for this try/catch block will not work, I know this because I have tested it.
When not logged in you will get the following:
ERROR: Please run 'az login' to setup account.
ERROR: The subscription of '<subscription>' doesn't exist in cloud 'AzureCloud'.
It happens because the Powershell try can not catch the error from an external program, so it does not get into the catch block and thereafter tries to set the subscription after the catch block (hence the 2nd error).
In #10602 I have solved this using the following way:
try {
az account show -o none 2>$null || Write-Error $_
Write-Verbose "Already logged in..."
}
The 2>$null writes the error to $null if not logged in to silence it, the pipeline chain operator || runs the next command because LASTEXITCODE is not 0, so the Write-Error $_ is there just to cause a Powershell error to get into the catch block.