Handling Azure PowerShell with multiple Azure Subscriptions


The other day I was trying to create an Azure WebSite for a client, I ran the commands, everything was successful. However when I went to the client Azure tenant, there was no sign of the web site! But I could hit the URL, which proved the site existed. I was dreading creating a Microsoft Support call, but as it was near the end of the day I went home. Deal with it in the morning. That evening, I was doing some work at home, and logged into my own personal Azure Tenant, and there was the Website I created for the client.

It turns out,

It doesn’t matter if you are logged into your Azure Website.

It doesn’t matter if you’ve just added the new account to Azure PowerShell.

It doesn’t matter if you’ve not used your old Azure Tenant in a while.

It Does matter what Azure PowerShell has as your Default, or Current Subscription at the time of calling your Azure cmdlets.

What do I mean?

Well before I explain Current/Default Subscription, let’s get those who have never used PowerShell Azure with their Azure account set up.

  1. Open your Azure PowerShell window
  2. Type
    Add-AzureAccount

    press enter.

  3. You will be asked to sign into your Azure account.

  1. Once done, PowerShell window will display your Id, Type of account, Subscriptions, Tenants.

If you now type

Get-AzureSubscription

it will state that your Subscription is the Default and Current. Default means every time you open your PowerShell window, it will already have this subscription loaded. Current means, if you have multiple subscriptions the one marked as current is the one you are performing your cmdlets against.

Selecting the Azure Subscription to work against.

So what to do? I’ve added a second, free trial azure subscription to powershell azure by calling the

Add-AzureAccout


cmdlet again. I’ve then called

Get-AzureSubscription


again to list all subscriptions. (Please note, you might have one account that have several subscriptions, it’s not a one to one relationship, just happens to be for me).

As you can see from the above image, the Subscription Name “Windows Azure MSDN – Visual Studio Ultimate” is both the default and the current subscription.

I can make the Subscription Name “Free Trial” my current subscription by calling (Note: Subscription name is case sensitive)

Select-AzureSubscription –SubscriptionName "Free Trial"

NOTE: (Just learnt this myself!!) If you have version 0.8.14 of PowerShell Azure you can call by Subscription ID instead. This will help for people with Multiple Subscriptions with the same name. Not sure which version this was implemented but in 0.8.3 it wasn’t there. Find out your version by typing (get-module azure).version

Select-AzureSubscription –SubscriptionId [SubscriptionID]

Now if you type

Get-AzureSubscription

you can see that my Free Trial is now Current.

To make the Free Trial default, all I need to do is add –Default to the Select-AzureSubscription cmdlet

Select-AzureSubscription  -SubscriptionName "Free Trial" –Default

Hopefully now you won’t end up making changes to the wrong Azure instance like I did originally.

Advertisements