Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Azure Automation Using PowerShell (Part 1)

DZone's Guide to

Azure Automation Using PowerShell (Part 1)

Love Azure but not a fan of working through the web console? Connect to your cloud through PowerShell instead. This series kicks off with some basic commands.

· Cloud Zone
Free Resource

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

If you've spent time with Azure, you're probably familiar with the Azure Web 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, we turn to automation to solve problems for us. To do that, we write scripts that keep running in order to ensure the valid logic gets executed.

For Azure to be automated, we need the Azure SDKs. So, for this lesson, if you don't have them already, go ahead and download them.

There are options for various tools and languages supported by Azure, like Java, Visual Studio, Node.js, PHP, Python, Ruby, etc.

But here, we are interested in PowerShell.

Image title



Once downloaded, click and follow the installation guide. That will allow Azure and PowerShell to work together.

Once the PowerShell-Azure SDK is installed, open PowerShell with administrator rights.
Then, the first thing we need to is log in with our 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 log in.

Image title



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

Image title


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

#To fetch subscriptions
> GetAzureRmSubscription

#To select a particular subscription
> GetAzureRmSubscription -SubscriptionName SubName | Select-AzureRmSubscription


To get the details of the subscription we are currently working on, our script looks like this:

> Get-AzureRmContext


You might have noticed AzureRm in the code snippets above. The “Rm” here means "Resource Management." Let's grab the list of our current subscription's VMs:

> Get-AzureRmVm


Image title


The above image shows that we have a single VM with the name “VS2015”. There is other information as well, like disk size, type, OS, etc.

Now, to stop an Azure VM, we need to use the following script:

> Stop-AzureRmVm -ResourceGroupName VS2015 -Name VS2015


Image title

And there you have it. You've got a few easy commands to connect to Azure and use the cloud's resources without using the Portal. In upcoming articles, we'll look into creating and using other services through PowerShell.

Site24x7 - Full stack It Infrastructure Monitoring from the cloud. Sign up for free trial.

Topics:
cloud ,microsoft azure ,powershell ,cloud automation ,tutorial ,azure virtual machines

Published at DZone with permission of Suraj Sahoo, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}