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.
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:
This opens up the Microsoft Azure Login window. Add the user name and password and log in.
After the login, in the PowerShell window, we can see the details of our subscription.
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:
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:
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
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.