My Azure account has two directories associated with it -- one for production and one for a testing environment. Currently, when I run Login-AzureRmAccount, it logs into the production subscription. Is there a setting I can configure on the Azure RM side so that when I run the Login-AzureRmAccount PowerShell cmdlet, it automatically logs in to the testing subscription?
I realize I could use Select-AzureRmSubscription to change the subscription, but I'm looking for a way to have it automatically default to my preferred subscription without having to run an additional command. (In fact, this is for use with a 3rd party tool, which automates the PowerShell commands, so I have no way to insert additional parameters or the Select-AzureRmSubscription call into the flow.)
3 Answers
There's currently no way to change default subscription for ARM unlike ASM's -default parameter.
Here's a workaround using Powershell profile:
Test if you have one already:
Test-Path $profile if False, create one:
New-Item -path $profile -type file –force Then add this to the file:
$azureAccountName ="your username" $azurePassword = ConvertTo-SecureString "your password" -AsPlainText -Force $psCred = New-Object System.Management.Automation.PSCredential($azureAccountName, $azurePassword) Login-AzureRmAccount -Credential $psCred -TenantId "your tenant id" -SubscriptionId "your subscription id" -Credential parameter only works with Azure AD tho. If you are using a Microsoft Account you need to remove it and manually login everytime.
3We can use select-AzureSubscription -Default -subscriptionname to set default azure subscription:
Select-AzureSubscription -SubscriptionName "Visual Studio Enterprise" -Default Then you can exit the PowerShell and test it, we can use
Get-AzureSubscription -Current to check the default subscription.
PS C:> Get-AzureSubscription SubscriptionId : 53847abb-xxxx-xxxx-xxxx-0361exxa7b15 SubscriptionName : Visual Studio Ultimate with MSDN Environment : AzureCloud DefaultAccount : [email protected] IsDefault : False IsCurrent : False TenantId : 1fcf418e-xxx-4c99-xxxx-d8e1xxf8737a CurrentStorageAccountName : SubscriptionId : 3b4d41fa-xxxx-xxxx-xxxx-13xx21b3b77d SubscriptionName : Visual Studio Enterprise Environment : AzureCloud DefaultAccount : [email protected] IsDefault : True IsCurrent : True TenantId : 67752319-xxxx-xxxx-xxxx-b820ec2377e0 CurrentStorageAccountName : 4After logging into Azure, using e.g. Connect-AzAccount in PowerShell (version 7+), or whichever command you use to login, you can use the Update-AzConfig -DefaultSubscriptionForLogin {} command in PowerShell to update the default subscription so that in the future, it will always choose your specified default subscription.
Example of changing the default subscription:
Update-AzConfig -DefaultSubscriptionForLogin YourSubscriptionNameHere You can check to make sure the default was changed by using the command:
az account show --output table