Save profiles for all Azure subscriptions in one go

As discussed in one of the previous blog posts, we can use PowerShell to help create persistent logins. Now consider scenario, where you have access to multiple azure subscriptions. Off course, you can download and save AzureRM profile for each one of the them. However, there are two major issues:

  1. AzureRM profile downloaded is associated with a token by default and it expires in a few days.
  2. If you have too many subscriptions, it can be tiresome to first select subscription and then save the profile.

Below is one of the ways that you can use to automatically save profiles for all of your associated subscriptions:

$SubscriptionNames = Get-AzureRmSubscription | Select SubscriptionName
foreach($SubscriptionName in $SubscriptionNames.SubscriptionName){
 Select-AzureRMSubscription -SubscriptionName $SubscriptionName
 Save-AzureRMProfile -Path "C:\$SubscriptionName.json"
}

Now, you can load all of these json files in your PowerShell profile and easily use them.

If you are latest version of PowerShell – please refer to https://mohitgoyal.co/2017/07/20/error-the-term-save-azurermprofile-is-not-recognized-as-the-name-of-a-cmdlet-function-script-file-or-operable-program/ for updated commands.

You can better use below code on new versions of Azure PowerShell:

$SubscriptionNames = Get-AzureRmSubscription | Select SubscriptionName
foreach($SubscriptionName in $SubscriptionNames.SubscriptionName){
Select-AzureRMSubscription -SubscriptionName $SubscriptionName
Save-AzureRMContext -Path "C:\$SubscriptionName.json"
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s