I have multiple azure tenants, each having multiple subscriptions, and I have to run a single PowerShell script for all my subscriptions.
This can be achieved using Azure CLI
, and it works perfectly.
I use Azure CLI
as below;
$az_account = (az account list --query "[].[name]" -o tsv)
foreach ($account in $az_account) {
az account set --name $account
#<RUN SCRIPTS HERE>#
}
But in some situations, I have to use the Az PowerShell
command instead of Azure CLI
.
So could anyone help me
- How to run
Az PowerShell
commands for multiple subscriptions - Or the
Az PowerShell
profile file path ( same asAzure CLI
which isC:Users%USER.AzureazureProfile.json
).
2
Answers
Resolution
After a couple of tries, finally found some methods to solve my issue. posting the same here might be helpful for someone.
You can connect all the subscriptions using the below commands
To List all the connected subscriptions
If you want to rename to a friendly Name,
To save these profiles to file,
You can import any time these profiles using the below command, (test first clear the profile
Clear-AzContext -Force
)Now you easily use
for
loop to set the subscriptions, for exampleThank you
You can use the below PowerShell Scripts to run the multiple subscription PowerShell commands.
Refer here for profile file path location