Friday, May 15, 2015

Managing Multiple Azure Subscriptions

Like many in the industry, I have multiple Azure subscriptions. When I create Azure resources, such as VMs, they are created in one of those subscriptions. However,  when I issue get-* cmdlets, such as Get-AzureVM, Azure only returns the objects in the 'current' Azure subscription, which can lead to some confusion (where did my VM go??).

Of course, when we look to making life easier for ourselves, we naturally turn to PowerShell. There is a cmdlet you can use to set a particular subscription to be the current subscriptions, which those Get-* cmdlets would then use. But that takes time – plus I seem never to remember the cmdlets and have to go discover the details.

To simplify things, I've written a short function that sets one of the subscriptions as current, which looks like this:

Function Set-CurrentAzureSubscription {
[CmdletBinding()]
Param($SubscriptionNumber)
# Check minimum sub number
If ($subscriptionNumber -lt 0) {return 'Try again'}

# Get azure subs
$Subs = Get-AzureSubscription

# check the upper bound
If ($SubscriptionNumber -GE $Subs.count) {return 'try again'}

# set the sub as current
$Subs[$subscriptionNumber] |Select-AzureSubscription -Current
# and say so
"Subscription [$($subs[$subscriptionNumber].SubscriptionName)] set as current"
}

Set-Alias CAS Set-CurrentAzureSubscription

With this function and alias, I just type: CAS 0 to select the first subscription, and (in my case ) CAS 2 for the final one. Here's a look at using this:

image

As you can see, setting my current subscription causes Get-AzureVM to return different VMs! This function is now part of my $profile.

No comments: