Posted by on Feb 25, 2015 in #Azure, #PowerShell | 0 comments

It’s easy to manage your Azure VM in the Azure portal – but what if we wanted to automate certain operations? Imagine the following scenario; you have a powerful VM that performs heavy data analysis at the end of every week. It makes little sense to keep this VM running during the week when it’s simply not performing any work. No one wants to pay for that!

Would it not be wonderful if we could keep the VM suspended until we actually need it? Surely you could head to the Azure portal and start/stop this VM every weekend, but hey – there’s a better way!

Go ahead and install the Azure PowerShell modules through the Microsoft Web Platform Installer: http://azure.microsoft.com/sv-se/documentation/articles/install-configure-powershell/#Install

Once you got it on your machine, search for Azure PowerShell and fire it up! If you’d like to get a list of all the Azure cmdlets available to you – type the following command and hit enter:

help azure

azps1

In order to start using these cmdlets we need to get our Azure subscription information imported. There are multiple ways that you can do this, but a neat approach is to simply import an Azure publish settings file. You can download this file by using the following command:

Get-AzurePublishSettingsFile

This is will open your web browser, let you sign in to Azure and initiate a download of the publish settings file.

Next up, import this file using the following command.

Import-AzurePublishSettingsFile “[LocalPathToFile]“

Replace [LocalPathToFile] with the actual path to the downloaded file. If the import is successful, you’ll be prompted with some basic information about your account. You can also list your Azure subscriptions with the following command:

Get-AzureSubscription

Make sure that the resource (the VM in this case) is located at the subscription that you’re using.

Let’s go ahead and start a stopped VM. Make sure to locate the name of your VM and the service name (of the Azure service on which the VM is located). In my case down below it’s pstestvm for both of the parameters.

azps2

To launch the operation, use the following command.

Start-AzureVM [Name] [ServiceName]

Obviously, replace [Name] and [ServiceName] with your own parameters. If you’d look in the Azure portal at this point, the VM would notify you that it’s starting.

If the operation is successful, you’ll get an OperationStatus of Succeeded – as below.

azps3

It’s just as easy to stop your VM – use this command:

Stop-AzureVM [Name] [ServiceName]

Again, replace [Name] and [ServiceName] with your own parameters. One thing to note with this operation is that your VM will lose its deployment IP – unless you flag it with -StayProvisioned.

In conclusion, there’s a massive amount of operations for you to perform on all of your Azure resources – not just your VMs. Check out the Azure Cmdlet Reference for an overview of what you can do: http://msdn.microsoft.com/en-us/library/azure/dn495184.aspx

Let’s get automating!

-Simon Jäger