
I’ve decided to put this quick post together to show you can use Azure Automation effectively to automatically schedule the Start-up/Shutdown of Azure VMs in parallel. This is extremely useful if you work on large environments where you need your VMs to power down/up in a particular order and time.
The idea here is to show you how you can implement a simple and scalable technique across all of your environments, that uses a tagging priority for start-up/shutdown and has the capability to power off your VMs in parallel on a repeatable basis. I’ve used this technique in large enterprise environments and it works as and when expected. So, let’s get into it…
1) Tag your VMs
This process is straightforward, there’s loads of information on the web on how to do this so i’m going to skip showing you. However, it’s really important to ensure your VMs are tagged as per my recommendations otherwise script won’t work as expected.
Each VM that you want to include within a particular resource shutdown/startup should have the following tags and an appropriate value.
ScheduledShutdown – This will be either ‘Yes’ or ‘No’. Any VMs that are tagged with ‘No’ will be excluded from the shutdown
StartupPriority – This number can be any number between 0 – 100. I recommend that you increment in blocks of 10 to allow for flexibility and growth. As an example, the following pattern could be applied for a multi-tiered application
Web Servers
ScheduledShutdown = “Yes”
StartupPriority = “70”
App Servers
ScheduledShutdown = “Yes”
StartupPriority = “60”
SQL Servers
ScheduledShutdown = “No”
StartupPriority = “50”
The “Shutdown” schedule will invoke the script and shutdown the VMs in parallel with the highest tags first before moving on to the next tag and finally the lowest tag.
Web Servers -> App Servers -> SQL Servers (Excluded as ‘No’ tag is set)
In reverse, the startup schedule will start the VMs with the lowest tagging priority first
2) Create a runbook
The Runbook will need to set as a PowerShell workflow. Ensure the Runbook name is the same as workflow name within the script.
3) Create and apply schedules
Create two schedules per resource group, one for Shutdown and one for Startup. In the following example i’ll provide screenshots for the Shutdown schedule.
- Decide on your schedule, a typical example would be 08:00 – 20:00 Monday to Friday (230 hours over a month)
- Select your time zone, i’m working from Australia at the present time. I’ve put some logic into the script to check for the time zone and also day of the week.
$a = (Get-Date).ToUniversalTime().AddHours(10)
$day = $a.DayOfWeek
if (($day -like “*Saturday*”) -or ($day -like “*Sunday*”)){
write-output “Weekend detected……”
}
If the weekend is detected, the script will exit without progressing with the script.
- Select recurring and set the frequency to “1 day” as per the following screenshot
Next you need to set the parameters within the schedule. These will be passed into the Runbook at the time of execution.
.PARAMETER Operation
Mandatory “Shutdown” or “Startup”
.PARAMETER ResourceGroup
Mandatory Name of the Resource Group that hosts the VMs
.PARAMETER IgnoreScheduleShutdown
Mandatory “true” or “false”
This is generally set to False, however when you want to exclude a particular resource group changing the parameter will ignore the schedule
.PARAMETER RunOnWeekend
Mandatory “true” or “false”
That’s it for this blog, I hope it will help you out. If so, please share or drop comments below
Sean