Monthly Archives: November 2014

Working with schedules in Azure Automation (and SMA)

The few of us that have started working with Azure Automation and/or SMA have probably noticed the lack of functionality that would equal a monitoring activity in Orchestrator.

I’ve seen some solutions which consist of a Runbook being set in an infinite loop with a sleep timer at the bottom.
In practice this method would replicated the desired functionality, that is, until the Runbook hits the maximum runtime value and kills the process. If you’re using Azure Automation this would also mean that you’ll be consuming Automation Minutes. 24/7.

What you’ll want to do with Azure Automation and SMA is to work with schedules to mimic a monitoring activity. And this can be a hassle to manage… Unless you’re using the right tools! 🙂
(Hint: Powershell.)

First we’ll want to create the required schedules. In 99% of the cases I work with hourly schedules at different minute intervals and I create them with the following script found at Technet Gallery.

If you use the “-Verbose” parameter you’ll get output on the schedule creation process.


PS C:\> GenerateAzureAutomationSchedules -AutomationAccount $AzureAutomationAccount -Verbose
VERBOSE: Found 35 Azure Automation Schedule(s) with same Hour Interval.
VERBOSE: Creted Azure Automation Schedule for "Every 1 Hour(s) at minute 0".
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 2" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 4" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 5" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 6" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 8" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 10" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 12" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 14" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 15" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 16" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 18" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 20" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 22" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 24" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 25" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 26" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 28" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 30" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 32" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 34" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 35" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 36" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 38" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 40" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 42" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 44" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 45" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 46" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 48" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 50" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 52" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 54" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 55" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 56" already exists.
VERBOSE: Azure Automation Schedule for "Every 1 Hour(s) at minute 58" already exists.

view raw

GAASoutput.txt

hosted with ❤ by GitHub

And lastly we’ll want to assign Runbooks to the required schedules so the Runbook executes at the right time with the following script:


# Set Azure Automation Account Name
$PSDefaultParameterValues += @{"*-AzureAutomation*:AutomationAccountName"=$AzureAutomationAccount.AutomationAccountName}
# Select one Runbook
$Runbook = Get-AzureAutomationRunbook | Out-GridView -PassThru
# Select one/multiple Schedule(s)
$Schedules = Get-AzureAutomationSchedule | Out-GridView -PassThru
# Assign Schedules to Runbook
$Schedules | ForEach-Object { Register-AzureAutomationScheduledRunbook -Name $Runbook.Name -ScheduleName $_.Name -Verbose }

By piping to “Out-Gridview -Passthru” I can easily filter all the schedules and select the ones I want. Oh, and they script writes the intervals into the Description property of the schedules. This makes life a bit easier if you want the Runbook to execute, for example, every 5 minutes.

AaSchedules

The result would look something like this in the Azure Management Portal:

AaSetSchedules

I hope this helps you to manage your Automation Runbooks in a more efficient way and lower your Azure bill!

Again, the script can be found at: https://gallery.technet.microsoft.com/Generate-Azure-Automation-1dab9193