Archive for Gregory Chambers

Automating VM Startup and Shutdown in Azure Resource Manager

This article summarizes the differences in automating starting up and shutting down virtual machines (VMs) between Azure Resource Manager and Classic mode.

Microsoft introduced Azure Resource Manager to facilitate management of your cloud assets in Azure, but given how new it is, it is often difficult to find exactly how to do that.

Earlier this year I created a new Azure VM for a client for use during business hours. The client requested the ability to automate the startup and shutdown of the VM when not in use, in order to save on costly compute time. Implementing a scheduled startup and shutdown of classic mode VMs has been documented and posted to various blogs. It was hard to find how to schedule startup and shutdown when the VMs are managed in the new Azure Resource Manager (ARM).

Classic method

The classic method of setting up auto-start up and shut down involves three steps: create an automation account and configure automation credentials, create runbooks that contain the PowerShell workflow scripts you want to execute, and attach one or more schedules to those runbooks. With Azure Resource Manager, the PowerShell workflow script you use in the second step is different.

In the classic method, you would use code similar to the following to set up your automation.

Startup script

workflow Start_MyClassicVM

{

$cred = Get-AutomationPSCredential -Name “MyAutomationCredential”

Add-AzureAccount -Credential $cred

Select-AzureSubscription “MySubscription”

$myVM = “MyVMName”

$serviceName = “MyService”

Start-AzureVM -ServiceName $serviceName -Name $myVM

}

First you fetch your credentials for your automation account and authenticate using the Add-AzureAccount method.

Once authenticated you designate which subscription context you are using. Often this might be called “Pay-As-You-Go” or “MSDN Subscription”.

You would then specify your service and run the Start-AzureVM command against your VM in that service.

Azure Resource Manager method

When using Azure Resource Manager you would adapt the following code.

Startup script

workflow Start_MyRmVM

{

$cred = Get-AutomationPSCredential -Name “AzureServiceAccount”

Add-AzureRMAccount -Credential $cred

Get-AzureRmSubscription -SubscriptionName “MySubscription” -TenantId “GUID-goes-here” | Set-AzureRmContext

$resourceGroup = “MyResourceGroup”

$myVM = “MyRmVm”

Start-AzureRmVM -ResourceGroupName $resourceGroup -Name $myVM

}

While configuring the automation account, runbooks and schedules remain the same, there are several key differences between automating startup of an ARM VM and a classic VM. Almost all of the methods you use with the Azure Resource Manager will contain “Rm” after the word “Azure” in their name. Also, the statement starting with “Get-AzureRmSubscription” is a bit more complex than in the classic version. Note that now, you need to know not just your subscription name, but also your Azure Active Directory Tenant ID. (Since I did not have access to the Azure Active Directory for my client’s subscription, I retrieved this GUID from the URL in the address bar after navigating to the Azure Active Directory in the classic portal. It’s possible, however, that this will change in the near future.) You then pipe the results of this new command, complete with the Tenant ID, to Set-AzureRmContext.

After that, rather than supplying a Service Name, you supply the name of your Azure Resource Group to the Start-AzureRmVM cmdlet.

Azure menu

Active directory link in Azure Portal menu

Shutdown script

To shut the VM down, the code is near-identical except for the last line:

Stop-AzureRmVM -ResourceGroupName $resourceGroup -Name $myVM -Force

Here is the full workflow script for shutdown.

workflow Stop_MyRmVM

{

$cred = Get-AutomationPSCredential -Name “AzureServiceAccount”

Add-AzureRMAccount -Credential $cred

Get-AzureRmSubscription -SubscriptionName ‘Pay-As-You-Go’ -TenantId “GUID”| Set-AzureRmContext

$resourceGroup = “MyResourceGroup”

$myVM = “MyRmVM”

Stop-AzureRmVM -ResourceGroupName $resourceGroup -Name $myVM -Force

}