Azure Automation Using PowerShell

Introduction

In order to run Azure, we know the Azure web portal where we can modify and manage resources as and when needed. But to manage resources on regular basis, logging into the portal each time is a hectic job. So, automation can solve most of these problems for us. For automation, we write scripts that keep running in order to ensure the valid logic gets executed.

For Azure to be automated, we need the Azure SDK. Download the SDK if not added already, from the Download SDK link. There are options for various tools and languages supported by Azure up till date, like Java, Visual Studio, NodeJS, PhP, Python, RUBY, etc.

Here we are interested in PowerShell Command Line tools.

Azure

Once downloaded, click and follow the installation rules and then the Azure contents would work with Windows PowerShell.

Once the PowerShell Azure SDK is installed, open PowerShell with administrator rights. Then, the first thing we need to do is to login with Azure credentials and connect to Azure.

> Login-AzureRmAccount 

This opens up the Microsoft Azure Login window. Add the username and password followed by hitting on Sign In.

Azure

After logging in the PowerShell window, we can see the details of the subscription.

Azure

Now, suppose you have multiple subscriptions and want to select a particular subscription, then the below command works perfectly.

//To fetch the subscriptions > GetAzureRmSubscription
//To select the particular subscription > GetAzureRmSubscription -SubscriptionName SubName | Select-AzureRmSubscription 

To get the current subscription details for which we are working on, the Shell script goes like,

//To get the current context of the azure subscription being used > Get-AzureRmContext 

The above few syntax have AzureRm. "Rm" here means 'Resource Management'.

Getting the list of VMs in the Azure resources under the subscription -

 > Get-AzureRmVm //Gets the list of VMs under the subscription 

Azure

The above image shows we have a single VM with Resource name "VS2015". There are other information as well, like disk size, type, OS, etc.

Now, to stop the Azure VM, we need to have the below running script.

> Stop-AzureRmVm -ResourceGroupName VS2015 -Name VS2015 

Azure

This way, we can use the PowerShell script and connect to Azure in order to use and create Azure Services without using the portal. In the upcoming articles, we will look into creating other services through PowerShell.

Up Next
    Ebook Download
    View all
    Learn
    View all