Schedule shutdown and start-up of Azure Virtual machine using Azure Runbooks and Automation Accounts

Create a scheduled automation runbook to start-up Azure Virtual machines

Step 1 – Create resource group

The following resource group was created

Step 2 – Create virtual machines

For test purposes two virtual machines were created (VM1 / VM2)

Attach the virtual machines to an existing virtual network, within this demo a test Vnet was created and both virtual machines were assigned to the same virtual network.

During the creation of the virtual machine the auto-shutdown was disabled (by default this is enabled but we want to use tagging to start and shut down the VMs)

NOTE: If only a few machines require scheduled stop and start, i recommend to use the built in option within Azure to ‘Auto-shutdown’ the VMs and then use the following runbook to start the VMs. Should you have multiple VMs which need shutting down at different times etc.. Then use the following shutdown runbook to shut the machines down using a tag rather than adding hundreds of auto-shutdowns on each VM manually.

Once both VMs have been created the will be shown within the resource group allocated

Step 3 – Assign Tags

You can either assign tags during the build process or add tags to any existing VMs already deployed. Highlight the VMs required and ‘Assign tags’

The following Tag was applied with the respective value:

Step 4 – Create Azure Automation Runbook

This can achieved by either creating the runbook and then importing the script to the runbook or by importing the script which in turns automatically creates a runbook. For this purpose we will create the runbook manually so it has all values required from the start.

Create the automation account from the azure portal:

The automation account was created within the same resource group as the VMs:

Once deployed the automation account will be available:

Step 5 – Create scheduled script

Use the following script:

## Get the Azure Automation Account Information 
$azConn = Get-AutomationConnection -Name 'AzureRunAsConnection' 

## Add the automation account context to the session 
Add-AzureRMAccount -ServicePrincipal -Tenant $azConn.TenantID -ApplicationId $azConn.ApplicationId -CertificateThumbprint $azConn.CertificateThumbprint 

## Get the Azure VMs with tags matching the value '6am' 
$azVMs = Get-AzureRMVM | Where-Object {$_.Tags.StartTime -eq '6am'} 

## Start VMs 
$azVMS | Start-AzureRMVM

Save the above script locally and then upload into the automation account. Browse into the azure automation account – runbooks – import a runbook

Then upload the locally saved script

NOTE – Change the Runtime version to 5.1

Once uploaded the powershell script will be available within the portal:

Step 6 – Create Azure Run as accounts

Within the automation account, browse to ‘Run as accounts’ and create an Azure Run as Account:

Select the create option and azure will automatically create this account:

Once created the account will be available:

Within the account the certificate can be renewed (when required):

Step 7 – Test the azure runbook

First action is to shutdown the virtual machines and ensure they are ‘Stopped’ within the Azure portal:

Browse back to the automation account and select the runbook, then the script uploaded previously and click ‘Edit’

Within the runbook options select ‘Test pane’

Once the Test pane is open, click on the Start button to start the testing. Wait until the runbook testing is completed, and you should see the output stream returned.

Wait for the output from the Test

Once the test had successfully passed the virtual machines will go from a stopped to a Running state:

Step 8 – Schedule the azure runbook

Within Automation accounts select Schedules – Add a schedule

The following values were entered:

Once created the schedule will be available:

Step 9 – Publish the Runbook

To publish a runbook in the Azure Portal, navigate to the runbook and open it in Edit mode.

Note – the current status is set to New

Now Publish the runbook:

The runbook status will now be published

Step 10 – Linking the schedule to the Runbook

Use the ‘Link to schedule’ option within the Runbook

Select the following option:

Now select the Schedule created earlier or any other schedules which may be set (should you be using multiple VM schedules)

Within the runbook the schedule will now be linked

Create a scheduled automation runbook to shutdown virtual machines

Should you decide to use an automation account and runbook to shutdown the virtual machines rather than the azure built-in option then follow the same process above but make the following amendments.

Create a script to stop the virtual machines:

Use the  same script previously but make the changes highlighted above.

Ensure you use the ‘-force’ command as this will suppress the confirmation window to shutdown the virtual machine.

Save the script and upload into the automation account.

Add the tags to the virtual machines which require the scheduled shutdown which matches the tag specified in the script:

Now the virtual machines will have two tags applied:

Now we can test the runbook by using the Test pane option and again once tested the output will be displayed:

When the test is running you will notice the machines Deallocating and then they will eventually be in a ‘Stopped’ state:

Next action is to create a new schedule and then link the runbook to the schedule (as shown previously)

Then publish the runbook to convert from status:

And now link the published runbook to the Stop schedule:

Summary

After reading this article, you have learned how to use Tags and Azure Automation to create a runbook to automate Azure virtual machines’ start-up and shutdown at a set schedule. You’ve learned the step-by-step process:

  • Assigning tags to virtual machines.
  • Creating, testing, and publishing a draft runbook.
  • Creating a schedule and assigning the schedule to the published runbook.