I have a set of small scripts written i PowerShell using the Az module that perform some maintenance tasks as part of a release pipeline in Azure DevOps. One of these scripts use the Azure CLI for one of its operations so I need to authenticate with both Azure PowerShell and Azure CLI in the same devops task in my pipeline.
I know I can split up the script and run one section from an Azure CLI task and another from an Azure PowerShell task, but I was hoping to keep it all clean and together. I prefer the clarity and tooling when using Az-PowerShell, but since there is not yet parity between az-cli and az-powershell I sometimes need to use the former.
I may also be able to read env vars from an Azure CLI task and transfer them to PowerShell to do az login from there, but I was hoping there was a cleaner way to do this. Ideally an AzureCLIAndPowerShell task or something like that.
Is there some way to perform a headless az login on a Microsoft hosted agent from within a script launched by an Azure PowerShell task?
Or to be more precise: Is there a way to use both Az-Powershell and Azure-CLI in an authenticated state within the same task in an Azure DevOps Pipeline?
So here is what I have done. This I have tested so switch to an Azure@CLI2 and this should get you access to both To authenticate via azure powershell from CLI Connect-AzAccount -KeyVaultAccessToken $(az account get-access-token --resource https://vault.azure.net --query accessToken --output tsv) -AccessToken $(az account get-access-token --query accessToken --output tsv) -AccountId $userId["value"]
If you want to read KV data -KeyVaultAccessToken is required.
You can reverse this some and get an access token from azure PowerShell, but I have not found a way to use it to login. In Azure PowerShell you can run (Get-AzAccessToken).Token That will give you the access Token. You might be able to do some tickery with it, but right now The easist method I would say is use the Azure@CLI2 pass in the servicePrincipal like normal and then use the above azure cli to Azure PowerShell.