Improved Select-AzureRmSubscription

Are you managing multiple subscriptions with a single corporate account?

I sometimes work with customers that uses multiple subscriptions in Azure, but uses the same Azure Active Directory, so you can atleast access them all from one account.

I searched the Internet and found this simple way of selecting the subscription from a list:

#Get Azure Subscriptions
{
            $subscriptions = Get-AzureRmSubscription
            $menu = @{}
            for ($i = 1;$i -le $subscriptions.count; $i++) 
            {
                Write-Host -Object "$i. $($subscriptions[$i-1].SubscriptionName)"
                $menu.Add($i,($subscriptions[$i-1].SubscriptionId))
                }
            [int]$ans = Read-Host -Prompt 'Enter selection'
            $subscriptionID = $menu.Item($ans)

            $subscription = Get-AzureRmSubscription -SubscriptionId $subscriptionID
            Set-AzureRmContext -SubscriptionName $subscription.SubscriptionName
            $subName = (Get-AzureRmContext).Subscription.SubscriptionName
}

Adding it to a PS module, and here the selection is the module, but of course the module could be anything.

Function Select-AzureRmSubscription {
 [cmdletbinding()]
Param(
        )
Begin {
        }
Process {
            #Get Azure Subscriptions
            $subscriptions = Get-AzureRmSubscription
            $menu = @{}
            for ($i = 1;$i -le $subscriptions.count; $i++) 
            {
                Write-Host -Object "$i. $($subscriptions[$i-1].SubscriptionName)"
                $menu.Add($i,($subscriptions[$i-1].SubscriptionId))
                }
            [int]$ans = Read-Host -Prompt 'Enter selection'
            $subscriptionID = $menu.Item($ans)
            $subscription = Get-AzureRmSubscription -SubscriptionId $subscriptionID
            Set-AzureRmContext -SubscriptionName $subscription.SubscriptionName
            $subName = (Get-AzureRmContext).Subscription.SubscriptionName
            }
End {
    Write-host "Finished" -ForegroundColor Green
    }
}

To finish it off, I would add Login-AzureRmAccount to the Beginning, but if you only have one account, I wouldn’t like to enter my credentials everytime I want to select a subscription:

Function Select-AzureRmSubscription {
 [cmdletbinding()]
Param(
        )
Begin {
        #Login to the Azure Resource Management Account
        Try {
            Get-AzureRmContext
                } 
            Catch {
                    if ($_ -like "*Login-AzureRmAccount to login*")
                    {
                      Login-AzureRmAccount
                    }
                  }
    }
Process {
            #Get Azure Subscriptions
            $subscriptions = Get-AzureRmSubscription
            $menu = @{}
            for ($i = 1;$i -le $subscriptions.count; $i++) 
            {
                Write-Host -Object "$i. $($subscriptions[$i-1].SubscriptionName)"
                $menu.Add($i,($subscriptions[$i-1].SubscriptionId))
                }
            [int]$ans = Read-Host -Prompt 'Enter selection'
            $subscriptionID = $menu.Item($ans)
            $subscription = Get-AzureRmSubscription -SubscriptionId $subscriptionID
            Set-AzureRmContext -SubscriptionName $subscription.SubscriptionName
            $subName = (Get-AzureRmContext).Subscription.SubscriptionName
            }
End {
    Write-host "Finished" -ForegroundColor Green
    }
}

What it does, is to try Get-AzureRmContext, but unless you are logged in, it will fail and based on that error message, it will make you run Login-AzureRmAccount. If your allready logged in with that PS Session, it will continue to present the subscription you have access in.

Thank you to everyone who shares their knowledge on the Internet.


Leave a Reply

Ehlo!

I am Roy Apalnes, a Microsoft Cloud Evangelist working av Sopra Steria. Main focus in Microsoft Security and Endpoint Management, with a bigger picture in mind.

Featured Posts