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:
- AzureRM profile downloaded is associated with a token by default and it expires in a few days.
- 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" }