fbpx
Dynamically Scheduled Runbooks in Azure Automation
By Steve Bowman
Published September 14, 2016
Estimated Reading Time: 3 minutes

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:

Install-Module AzureRM

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:

$Credential = Get-Credential
Login-AzureRMAccount -Credential $Credential

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:

$StartTime= (get-date).AddMinutes(10)
New-AzureRMAutomationSchedule –AutomationAccountName <Automation Account Name> –Name "Start in 10 min" –StartTime $StartTime -Onetime -ResourceGroupName <Resource Group Name>

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:

$Value1 = <Insert Parameter Value>
$Value2 = <Insert Parameter Value>
$RunbookName = <Azure Automation Runbook Name>
$ScheduleName = <Name of Schedule Created in the above cmdlets>
$ParamString = "Parameter1=$Value1 `n Parameter2=$Value2"
$Params = @{"Parameters"="$ParamString"; "RunbookName"= $runbookName; "ScheduleName"=$scheduleName}
Register-AzureRmAutomationScheduledRunbook –AutomationAccountName <Automation Account Name> –Name "Start-ScheduledRunbook" –ScheduleName $scheduleName -ResourceGroupName <Resource Group Name> -Parameters $Params

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!

Post Tags:
Article By Steve Bowman
Steve Bowman is a Partner at Model Technology as well as their Vice President of Sales and Marketing. Steve is a father, husband, Franciscan, and lover of technology. He's bilingual in business and technology and have over 30 years of experience in selling enterprise technology solutions in a variety of industries.

Related Posts