Dynamically Scheduled Runbooks in Azure Automation

Hello again, Automation fans.

 

Today we’re going to be talking about Dynamic Schedules and Runbooks in Azure Automation, using AzureRM. This is an often requested topic, since the Azure Automation portal only offers the capability to schedule runbooks to run every hour, and does not allow for slices within the hour or for runbooks to run more than once an hour.

Using a series of PowerShell cmdlets, we will be able to create schedules to run whenever you please.

 

First things first, if you are using a local client instead of the Azure Automation portal, you will need to install the AzureRM module. This can be accomplished with the following simple command:

If you are using the Azure Automation portal, the above step can be skipped as the module is already loaded.

 

The next step will be to authenticate the session into Azure Automation. This can be done using the following commands:

NOTE: The credential can be subbed out for any alternative way of providing a PSCredential Object into a script (Ex: $Credential= Get-AutomationPSCredential -Name ‘Credential-Variable’)

 

Now that the session is authenticated into Azure, we will be working with two cmdlets: New-AzureRMAutomationSchedule and Register-AzureRmAutomationScheduledRunbook.

These cmdlets are used to create a new schedule with a specified time and link it to a runbook, respectively.

 

For the New-AzureRMAutomationSchedule command, there are a few parameters you will need to provide, and those are detailed very well in the TechNet Article for the command. One thing to note, is that you can provide the Start Time variable. This allows us to have greater control over the schedule time than the portal allows. For example, if you want to create a schedule for 10 minutes from the current time, you can use the following command:

Once you have your schedule created, you are now able to link it to a runbook. The parameters required for the Register-AzureRmAutomationScheduledRunbook are also detailed in the TechNet Article.

 

There is an important thing to note with this cmdlet however, and is something we’ll be addressing a workaround for. The Register-AzureRmAutomationScheduledRunbook does not allow you to specify the Hybrid Runbook Worker you wish to run a runbook on. Because of this limitation, all runbooks scheduled using this command will always run in Azure, never on premises. Since this is a severe limitation in certain situations, we will want to use a workaround for this.

 

The solution I created to address this problem was a new runbook, one that would handle the process of starting a runbook on a Hybrid Runbook Worker, but that could run in Azure. The runbook is attached at the bottom of this post. The way it functions is to act as a “middle-man” between the runbook creating the schedule and the runbook being scheduled.

 

The format to use it is as follows:

The $RunbookName variable should be set to the desired target runbook, and the $ParamString variable should be configured with the desired parameters for that target runbook. Additionally, the $ParamString variable is formatted in a way that will be parsed by the ConvertFrom-StringData cmdlet in the Start-ScheduledRunbook script. All desired parameters must be separated by `n. If there is only one parameter, “Parameter1=$Value1” will suffice.

 

By using this method, the schedule will start the targeted runbook using the parameters specified, and run on the designated Hybrid Runbook Worker.

 

This allows for one runbook to schedule another using a desired timeframe instead of hourly segments, and allows the scheduled runbook to run on a Hybrid Runbook Worker.

 

Start-ScheduledRunbook Download:

ZIP Format: Start-ScheduledRunbook.zip

TXT Format: Start-ScheduledRunbook.txt

 

Good Luck, and Happy Scripting!

By |2016-09-14T14:47:57+00:00September 14th, 2016|Azure, PowerShell|0 Comments

About the Author:

Model Technology

Let us help you get your end point and data center strategy on cruise control!  Ask about our Calibration Assessment.

CONTACT US

  • 11500 Olive Blvd Suite 260 Creve Coeur, MO 63141
  • (314) 254-4138
  • sales@model-technology.com

RECENT TWEETS