Using Azure Automation and SharePoint Online lists to schedule VM start-up and shutdown

To make the most efficient use of Public cloud you need to ensure your servers are only running when they need to be.
Using SharePoint online as the source of virtual machine start-up and shutdown schedules is an option that allows for an easy, consolidated view of all your VMs and their schedules plus allows for the granular allocation of permissions for your staff, without having to grant them any Azure fabric access.

I'm going to assume some familiarity with SharePoint Online custom lists and Azure Automation principles for this blog post.
If you need some help getting started with these, try the following links.
How to create a Custom List in SharePoint Online Getting Started with Azure Automation

To integrate SharePoint Online with Azure Automation (and Powershell scripts), I've used a fantastic module written by Tao Yang, SharepointSDK.
The details can be found here.

To get set up for using SharePoint Online and Azure Automation, please refer to my Blog post here --> https://blogs.msdn.microsoft.com/jrt/2017/04/07/setting-it-up-sharepoint-online-lists-with-azure-automation/

  • Create the SharePoint Custom list for your VMS
    The columns you'll need are

    • VMName {Renamed Title column} - 'Single line of text'
      This will be the Name of your VM in Azure
    • SubscriptionGUID - 'Single line of text'
      The Azure Subscription GUID where the VM is situated
    • ResourceGroup - 'Single line of text'
      The Resource Group Name of the VM
    • TimeZone - 'Choice'
      A drop down list of the available Timezones for the shutdown times to be run against {See note below}
    • ShutdownTimeRange - 'Single line of text' or 'Choice'
      This is a comma separated list of standard date formatted values using a ~ between times
      i.e. '6AM~6PM,Saturday,Sunday' or '25 April, 25 December, 1 January'
    • OverRide-Start - 'Yes/No'
      A checkbox indicating to force VM to be Started
    • OverRide-Stop - 'Yes/No'
      A checkbox indicating to force VM to be Stopped {takes precedence over 'OverRide-Start'}
  • Note: To create the list of available Timezones I used the following snippet of Powershell script -

    ForEach ($TZ in ([System.TimeZoneInfo]::GetSystemTimeZones())) { Write-Output "$($TZ.DisplayName) [$($TZ.Id)]"}

  • (Optional) Create the SharePoint Custom list for logging Automation Actions.
    The columns you'll need are

    • VMName {Renamed Title column} - 'Single line of text'
    • SubscriptionGUID - 'Single line of text'
    • ResourceGroup - 'Single line of text'
    • AutoActionLog - 'Multiple lines of text'
  • Create a script or Azure Automation runbook to populate the SharePoint List with your VMs

The best way to populate your SharePoint list is to either run a Powershell script or create an Azure Automation Runbook to do that.
I've posted a script to do this here --> Load Azure VMs into a SharePoint Online List
and an Automation Runbook that can be imported here --> Load Azure VMs into a SharePoint Online List - Automation Runbook

Alternatively you can just enter the required Virtual Machine details directly into the SharePoint List.

  • Lastly, the actual automation script that does all the work.

Download Link from TechNet.

The most complex piece to this is the TimeZone adjustments.
Azure Automation runs on UTC time so you need to make sure when you specify to shutdown a VM at 6pm on a Friday, you don't end up shutting down your VM at 8am on Friday, which is what happens using AST (Sydney, Australia) time!

I got myself lost when trying to make a small change that I had to stop all code development and actually write a Flow Chart of how script works.
I strongly suggest if you are trying to work out how this script does what it does, that you work with this Flowchart open alongside.
AutoShutdown-Flowchart  (Note: Please ping me if you want the Visio version of this to update any changes of your own)

  • To have this run regularly to check all VMs for their required state simply add a schedule for the Runbook.
    I would suggest running it at 5 minutes past the hour to ensure it picks up any machines correctly that need to be stopped or started.
    The frequency of the schedule will depend on how many different timeframes you need to use across all your VMs.