Creating custom runbooks from start/stop VM solution for specific sets of VMs using tags for sequenced start/stop

Azure provides a start/stop VM solution that allows you to stop/start VMs on a schedule; it consists of multiple PowerShell runbooks.

This solution offers three options when deployed:-

  • Schedule VMs to start and stop
  • Schedule VMs to start and stop in ascending order by using Azure Tags
  • Autostop Vms based on low CPU usage

Overtime you may have multiple sets of VMs that require to be scheduled to stop and start at various times in a specific order throughout the day and week; with this requirement, we will be looking at scheduling VMs to start and stop in ascending order by using Azure Tags.

Sounds great, although as mentioned you may have requirements for several applications to have a different stop/start times, this is where this blog post will show how you can edit the provided runbook and create your own for various application requirements.

I am going to show you how you can modify the initial runbook that allows you to “Schedule VMs to start and stop in ascending order by using Azure Tags” and detail how this can be edited to allow you to create multiple versions that can be used to stop/start various sets of VMs within your subscription.

What is required to run this solution?

  • Azure Automation Account
  • Log Analytics Workspace
  • The runbook requires an Azure Run As Account, details on how to create this account along with relevant account permissions found here

Enabling the solution

Within your Automation Account, select Start-Stop VM

Select Manage Solution

Search marketplace for Start/Stop VMs during off-hours

Select solution and follow onscreen process of enabling (this will take a few minutes)

Creating and modifying the Sequenced Start Stop runbook

Once the solution has been deployed, you will see a number of new runbooks within your Automation Account

As mentioned, I will be modifying/creating a new runbook taking a copy of the SequencedStartStop_Parent as my template, take a copy of the PowerShell script within this runbook

Create new runbook as below and copy the PowerShell script into this runbook:-

Now we have the base PowerShell template; what should I edit?

I want to keep the base template the same but change/modify variables to apply me to use the same script but with different variables to allow me to power off/on sets of VMs on different schedules

What should I modify within the script?

As this script uses VM tags to determine the power off and start sequence (1,2,3… etc), I will be using different tags depending on the VM set. For example

  • App1 VMs will have two tags “tamops-app1start” & “tamops-app1stop” with a sequencing number for each tag
  • App2 VMs will have two tags “tamops-app2start” & “tamops-app2stop” with a sequencing number for each tag

Within the PowerShell script, the below variables need to be changed depending on which app the runbook will be powering off & on as below:-

    $startTagValue = "tamops-app1start"  
    $stopTagValue = "tamops-app1stop" 

We are nearly ready to create a runbook schedule for App1; but depending on the size of your subscription you may want to filter the script further in relation to Resource Groups it will check for VMs with these specific tags. To do this, modify the below:-

$StartResourceGroupNames = Get-AutomationVariable -Name 'External_Start_ResourceGroupNames'
$StopResourceGroupNames = Get-AutomationVariable -Name 'External_Stop_ResourceGroupNames'

Note:- These are automation variables found in Automation Account -> Variables

By default, the solution has a wildcard “*” to search all Resource Groups within your subscription

Change or add new variables for specific Resource Groups for each app, with comma seperated; such as:-

resourcegroup1, resourcegroup2, resourcegroup3

Save the runbook changes and remember to publish the newly modified runbook

Test new runbook

Review the VMs with the app you want to stop/start as a test; ensuring they all have tag values

Within your newly created runbook, click start and configure parameter action to either “STOP” or “START”

Once you proceed you will be able to review the jobs output

Reviewing VM, it has been stopped

Creating a runbook schedule

Tested the runbook successfully, time to create an automated schedule

Within the newly created runbook, select schedule and create a schedule for the VMs to stop or start (two schedules are required, one to stop and one to start )

With schedules now created, the runbook has been configured successfully and the VMs will power off/on on a specific schedule

A handy solution to assist you with automating stopping and starting VMs on an application or set of VMs specific/tag basis

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s