Azure Resource Manager PowerShell Login — make it persist!
With multiple Azure subscriptions within a single Azure account, it is crucial to be logged into the correct Azure subscription (AzureRM Context), to be able to access the Azure resources within a specific subscription, via PowerShell (POSH). There is a default subscription that is set to open with each new POSH session. If this default subscription isn’t the preferred working subscription, you will have to select the correct subscription for every new POSH session.
With the Azure Context Autosave feature (added Sept 2017), it is possible that after setting the subscription for the current session, you can have the Azure credentials, account and subscription information saved and automatically loaded when you open a new POSH window — by using the Enable-AzureRmContextAutosave cmdlet
And for easy subscription selection, I recently found these simple POSH cmdlets here, to be able to brilliantly select the correct Azure Subscription by clicking on the list of Azure subscriptions in the Azure Windows account. No copying and pasting of subscription ID, name and/or tenant ID required!
# Login to Azure and choose the correct subscription using these cmdlets:
Login-AzureRmAccount$subscriptionId = (Get-AzureRmSubscription | Out-GridView `
-Title "Select an Azure Subscription and click OK…" `
-PassThru).SubscriptionIdGet-AzureRmSubscription -SubscriptionId $subscriptionId | Select-AzureRmSubscription
There are 3 subscriptions on this Azure account showing on the Gridview popup — I’ve selected the MVP_Azure — subscription:
#Set the selected subscription as the new default opening subscription with persisted credentials
Enable-AzureRmContextAutosave
#Verify the change to the new AzureRmContext
Get-AzureRmContext
To verify the persistance of the credentials and correct default subscription, close the POSH session and restart a POSH ISE and instead of having to log in to Azure again just input:
Get-AzureRmContext
Note that the Azure credentials, account and subscription were saved between sessions and automatically opened:
For more information on the ‘power’ of Azure Context Autosave, read Persisting user logins across Powershell sessions
Originally published on https://arlanblogs.alvarnet.com on April 20, 2018