Using Azure Automation to stop/start virtual machines

Today, we’ll see how we can leverage Azure Automation to stop/start virtual machines in Azure. We’ll begin with how to start virtual machine first and same steps can be applied to stop virtual machines. You’ll only need to change few commands in the runbook associated.

Login into Azure Resource Manager with your credentials. Click on Azure Automation account and then click on the runbooks section highlighted:

azure automation account properties
azure automation account properties

This will open up a new blade containing attached runbooks for this account. By default, you can see the 4 runbooks auto-created by Microsoft.

existing runbooks in the azure automation account
existing runbooks in the azure automation account

As you can see, this blade contains two buttons to add runbooks:

  1. Add a runbook: You can add either a new runbook or import an existing runbook
  2. Browse gallery: Use this option to use existing runbooks loaded on Azure site by Microsoft and community members.

At the top of the this blade, click Browse Gallery button. We can use built-in runbooks created by Microsoft for this simple task. Here’s what the gallery would look like:

microsoft gallery containing runbooks
Microsoft gallery containing runbooks

For the purpose of this blog, we’ll use ‘Start Azure classic VMs’ highlighted above. Click on the runbook and in the new blade opened, click import:

import runbook to use for starting azure virtual machines
import runbook to use for starting azure virtual machines

After this, we’ll need to edit the runbook we just imported. for this, just click on the runbook  and then click edit:

editing runbook Start-AzureVMs
editing runbook Start-AzureVMs

Now, if you edit code for associated PowerShell workflow, you can see that this workflow requires 3 optional parameters:

  1. AzureCredentialAssetName: This will be used to fetch the resources using Azure credentials
  2. AzureSubscriptionIdAssetName: This will be used to associate the subscription id with the azure account so as to get resources associated.
  3. ServiceName: This will be used to get VMs related to a specific cloud service only.

Out of these, though first two are optional, you need to have them as they will be used to get resources from Azure. Without these default values will be used. For now, just go ahead and publish the runbook. This will make runbook available for use.

Now, go to Azure Automation account blade again, then click on the assets. This will open up a new blade containing various tiles. You need to update values for AzureCredentialAssetName in the credentials section and AzureSubscriptionIdAssetName in the variables section:

Update values for parameters for runbook in respective tiles for variables and credentials
Update values for parameters for runbook in respective tiles for variables and credentials

Once you do this, go to Azure runbook -> Start-AzureVMs runbook -> Start. This will open up a blade showing real time information for the job status. You can see whether the job succeeded/failed and associated logs, if any.

Status of the job for azure automation runbook
Status of the job for azure automation runbook

For stopping Azure VMs, you can import associated runbook and then follow steps as mentioned above.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s