Geeks With Blogs

Kyle Burns
Microsoft has a great collection of pre-built solutions for Azure Automation that you can leverage to realize many common use cases. One of these common scenarios is when a company has many virtual machines that are only producing business value during specific times and the company wants to avoid paying for the time that the virtual machines are not producing value. The "Start/Stop VMs during off-hours" solution is an easy to configure way to meet this need without writing a line of code, but a customer of mine recently was talking with me about how it wasn't quite flexible enough to meet their needs because they needed more flexibility and didn't want to have to manage lists of virtual machines. That got me started thinking about how I would solve the problem for myself given the following goals:
  • Use tags to dynamically identify machines to turn on/off 
  • If possible, continue to benefit from the work that was put into the OOB solution
  •  Do not modify anything in the existing solution (that's asking for problems when it comes time to update to a new version)
  •  Allow for multiple schedules to manage different machines
  •  Provide "opt-in" by tagging either at the machine level or at the resource group level for all of the machines within the group
  •  Provide "opt-out" by tagging at the machine level to provide an override of the resource group setting for an individual machine
With this requirements mapped out, I installed and started studying the default solution a bit and found that the developers did a great job of modularizing the solution, with a "parent" runbook responsible for managing the list of machines to be operated upon and a "child" runbook to handle operations at the machine level. Since the only thing that I wanted to change was the machine selection criteria, this meant that all I needed to do was write a new "parent" runbook specific to my needs and continue to use the existing "child" runbook.

The new parent runbook took the following approach:
  •  Query virtual machines by the tag and add them to an "inclusion" hashtable
  •  Query resource groups by the tag
    •  For each tagged resource group, query all of the virtual machines in the resource group and add to the inclusion hashtable
  •  Query virtual machines by exclusion tag and remove them from the inclusion hashtable
  •  Loop through the inclusion hashtable, calling the existing child runbook for each virtual machine
The solution that I came up with can be found on my GitHub at https://github.com/kyleburnsdev/powershell-samples/blob/master/azure-automation/TaggedStartStop_Parent.ps1. I hope it provides you either some direct value or gives you ideas on how you can extend the default automation solutions. Happy hacking!

Posted on Friday, November 22, 2019 8:34 AM Azure , PowerShell , Automation | Back to top


Comments on this post: Extending the Start/Stop VMs during off-hours solution

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Kyle Burns | Powered by: GeeksWithBlogs.net