Set Up Your Azure Environment Easier in PowerShell

PROBLEM: You want to set your Azure context faster from within a PowerShell session.

SOLUTION: Creatively apply the Out-GridView cmdlet.

Here’s something for you to try:

  1. Open an elevated PowerShell console.
  2. Run Login-AzureRMAccount to log into your Azure subscription.
  3. Run Get-AzureRMContext.
  4. Here’s what I see on my Windows 10 administrative workstation:

Get-AzureRMContext

Environment : AzureCloud
Account : tsw202@comcast.net
TenantId : 44a7-4037-8eea
SubscriptionId : 3d935138-40b5-408c-98e9
SubscriptionName : WrongSub
CurrentStorageAccount :

WrongSub isn’t what I want to use as my current subscription, and Azure doesn’t show a default Azure storage account, either. Let’s sort out these problems.

Introducing…Out-GridView

The Out-GridView cmdlet sends your pipeline output into a separate window. Normally, the GridView window is non-interactive, as shown in the following example:


Get-Service | Where-Object -FilterScript { $_.Status -eq 'Stopped' } |
Out-GridView -Title 'Stopped Services'

out-gridview
Figure 1. Out-GridView in Windows PowerShell.

However, the -PassThru switch parameter puts your selection back into the pipeline for further processing.

To illustrate, look at what I’ve done here with Get-AzureRMSubscription:


$subscriptionId =
(Get-AzureRmSubscription |
Out-GridView `
-Title "Select an Azure Subscription..." `
-PassThru).SubscriptionId


NOTE: Break a line in a PowerShell script by using the pipe (|) or a backtick (`) characters. Be sure not to include additional space after the backtick, though, because you want to escape only the newline.


Let me explain what’s happening in the preceding pipeline:

  1. Create a variable named subscriptionID that holds our chosen Azure subscription ID
  2. Fetch all Azure subscriptions associated with the authenticated identity
  3. Push output into a grid window and wait for a choice
  4. Pack the selected Azure subscription into the SubscriptionID property of the PSAzureSubscription object.

Our next step is to pass the subscriptionID variable data into Select-AzureRMSubscription like so:


Select-AzureRMSubscription -SubscriptionID $subscriptionID

Now Let’s Set Our Resource Group and Storage Account

The same logic applies to setting our session default resource group…


$rgName =
(Get-AzureRmResourceGroup |
Out-GridView `
-Title "Select an Azure resource group..." `
-PassThru).ResourceGroupName

…as well as specifying our default storage account:

$stName =
(Get-AzureRmStorageAccount |
Out-GridView `
-Title "Select an Azure storage account.." `
-PassThru).StorageAccountName

Set-AzureRmCurrentStorageAccount -Name $stName -ResourceGroupName $rgName

Finally…The Correct Context

Alrighty then! Let’s verify that we have the proper Azure session context:


Get-AzureRMContext

Environment : AzureCloud
Account : tsw2002@comcast.net
TenantId : 44a7-4037-8eea
SubscriptionId : 7be05db5-0dea-4ffe-b309
SubscriptionName : RightSub
CurrentStorageAccount : azuretrainingstorage

For Further Learning

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s