Questions? Feedback? powered by Olark live chat software
Přeskočit navigaci

Scheduling Azure Automation runbooks with Azure Scheduler

Publikováno dne 27 ledna, 2015

Program Manager, Azure Automation
UPDATE 8/5/2015: With the introduction of Webhooks in Azure Automation, it is now much easier to schedule runbooks using Azure Scheduler. Please refer to this Azure post for additional details on the process. The older outdated method is below: Azure Automation has a scheduler that allows you to start Automation runbooks daily, hourly, or at a single point in the future. However, some customers may want to use the advanced scheduling features that Azure Scheduler offers to start runbooks. Examples include running scheduling runbook jobs on specific days or at more granular level, like minutes. In this post, I will describe how to create a new Azure Scheduler job that starts any runbook in your Azure Automation accounts. Below is the high level process that is involved in setting up the Scheduler job that will start an Automation runbook. SchedulerDiagram The first thing you need to do is to create an Automation runbook that you will associate with an Azure Scheduler job. I went ahead and imported a runbook from the gallery that checks if it is currently a work hour. This is just a sample to show how you can call a runbook from a Scheduler job. You can use any runbook you have in your automation account. The image below from the Automation import runbook wizard shows how to select a runbook from the gallery. GalleryItem   If you import this runbook into your Automation account, you will see that it takes two parameters, MyWeekDayStartHour and MyWeekDayEndHour and then returns true if it is currently between these two values or false otherwise.   Get-IsWorkHours After you have published this runbook, it will be available to be started by an Azure Scheduler job. In order to make it easy to use Scheduler to start an Automation runbook, I have added a utility runbook to the Automation gallery that will create or update an Azure Scheduler job in a job collection you have already created or it will create a new collection if it does not exist. The utility runbook is called Set-AzureScheduleWithRunbook, and you can import it from the Automation gallery “Utility” category. This runbook takes a number of parameters that are required to correctly associate a runbook with a new Azure Scheduler job. The following Automation assets must be created before you can start this Runbook.

AzureCertificateAssetName

This is a credential certificate asset that contains the management certificate that has access to the Azure subscription. This certificate must be marked as exportable when adding it to the Automation service. You can learn how to create an Azure .pfx certificate to call into Azure on http://msdn.microsoft.com/en-us/library/azure/gg432987.aspx. This certificate will be sent to the new Azure scheduler job so that it can call into the Automation service to start the runbook. UploadCertificate

AzureCertificatePasswordAssetName

This is an Automation variable asset that contains the password to the management certificate. This is the password value you entered when you added the certificate to the Automation Assets. This is required so that we can export the certificate from the automation service and enable the scheduler cmdlet to decrypt the certificate and send it to the scheduler job. AzureCertificatePassword

AzureADCredentialAssetName

This is a credential asset containing an Azure AD account username and password with access to the Azure subscription. You can read how to set this up on https://azure.microsoft.com/blog/2014/08/27/azure-automation-authenticating-to-azure-using-azure-active-directory/ . This allows the Automation service to authenticate to your Azure subscription and create a new Scheduler job using the Azure PowerShell cmdlets that ship in Azure Automation.AzureCredentialAssetName Once you have set up these Automation assets, you can import the runbook from the gallery into your Automation account and publish it. In order to use the Set-AzureScheduleWithRunbook runbook, you will want to call it from a parent runbook in which you will set up the required parameters for the Set-AzureScheduleWithRunbook runbook, and then pass them in a call to Set-AzureScheduleWithRunbook. You can create this parent runbook using +New in Azure and selecting to Quick Create a runbook.  Call this new runbook Configure-AzureScheduleParameters. You can then copy the example that is shown in the Set-AzureScheduleWithRunbook runbook .EXAMPLE comment section, and paste it into this new runbook like below:  
Workflow Configure-AzureScheduleParameters
{
    $RunbookName = "Get-IsWorkHours"
    $RunbookParameters = @{"MyWeekDayEndHour"=18;"MyWeekDayStartHour"=6}

    Set-AzureScheduleWithRunbook `
        -AccountName finance `
        -AzureCertificatePasswordAssetName CertPassword `
        -AzureCertificateAssetName AzureCert `
        -AzureADCredentialAssetName AzureCred `
        -Runbook $RunbookName `
        -Parameters $RunbookParameters `
        -SchedulerJobCollectionName FinanceJobCollection `
        -SchedulerJobName FinanceDaily `
        -SchedulerLocation "South Central US" `
        -SubscriptionName "Visual Studio Ultimate with MSDN"
}
  You can see in this example that I have set the $RunbookName to Get-IsWorkHours  and the parameters for MyWeekDayStartHour and MyWeekDayEndHour in the $RunbookParameters hashtable. It then calls the published Set-AzureScheduleWithRunbook runbook and passes in the required parameters to create a new scheduler job that will start the Get-IsWorkHours runbook. You will have to modify these parameters to match the names of the assets you create in the Automation service. The new scheduler job should look like below within the Azure portal of Scheduler if the runbook completes successfully. SchedulerJob You can now modify this scheduler job to make it a recurring job for any specific scheduling you need. This utility runbook Set-AzureScheduleWithRunbook is available in the Automation Gallery for you to import and start doing advanced scheduling and integration with Automation! It would be great to hear what automation tasks you do on a schedule, so we can make these as simple as possible. Let us know in the comments below. Just getting started with Azure Automation? Learn about the service here, and follow Azure Automation on Twitter.