Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Hosted-services / Azure

Azure Automation using Powershell

5.00/5 (1 vote)
31 Jul 2017CPOL2 min read 5.7K  
Azure automation using Powershell

Introduction

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

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

Here, we are interested for Powershell command line tools.

Image 1

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 the Powershell with administrator rights.

Then, the first thing we need to do is Login with Azure credentials and connect to Azure. The command is:

> Login-AzureRmAccount

This opens up the Microsoft Azure Login window. Add the user name and password and login.

Image 2

After the login, in the Powershell window, we can see the details of the subscription.

Image 3

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

//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, then the shell script goes like:

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

The above few syntax have AzureRm. Now the “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

Image 4

The above image shows that we have a single VM with Resource name “VS2015”. There is 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

Image 5

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)