Azure 360: PowerShell Commands

Hi Everyone. Let’s explore some PowerShell commands to make our VM creation script more functional and scalable.

Once we logged tin to Azure account with:

PS:>Login-AzureRmAccount

We need to select which subscription we would like to perform the installation to. (If you have a single subscription, this step is not required).

In my case, I have 3 subscriptions:

power1

The PS command:

Select-AzureRmSubscription -SubscriptionId xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

Will determine which subscription we would like to focus on.

 

The PS Command Read-Host will stop the running of the script and will wait for input from the user.

 

Option 1:

Get-AzureRmSubscription
$subscriptionID = Read-host “Please type a subscription ID:”

 

This will display a list of all the subscriptions that the logged in user have access to and will prompt the user to type the preferred SubscriptionID. This will be passed on to a variable called $SubscriptionID

Although, this option will work, it is prone for mistakes. It can also be challenging to get the right SubscriptionID in case there are many subscriptions attached to the account.

 

Option 2:

$subscriptionId =

    (Get-AzureRmSubscription |

     Out-GridView `

        -Title “Select an Azure Subscription …” `

        -PassThru).SubscriptionId

Select-AzureRmSubscription `

    -SubscriptionId $subscriptionId

 

The above code will display the list of subscriptions available in a “Out-GridView” which is a pickup window:

power2

This will enable the user to choose the correct subscription and will pass it to the $SubscriptionID variable.

Here is a script that configures few required variables for creating a VM in Azure:

 

# Authenticate to Azure Account

Login-AzureRmAccount

# Select an Azure subscription

 

$subscriptionId =

    (Get-AzureRmSubscription |

     Out-GridView `

        -Title “Select an Azure Subscription …” `

        -PassThru).SubscriptionId

 

Select-AzureRmSubscription `

    -SubscriptionId $subscriptionId

 

 

# Select existing Resource Group in which VM will be installed

$location =

    (Get-AzureRmLocation |

    Out-GridView `

        -Title “Select the Location …..” `

        -PassThru).DisplayName

 

# Select existing Resource Group in which VM will be installed

 

$ResourceGroupName =

    (Get-AzureRmResourceGroup |

    Out-GridView `

        -Title “Select Resource Group ……….” `

        -PassThru).ResourceGroupName

 

Next week I will discuss additional variables required and provide the complete script.

Have a great weekend!