Summary: Schedule an Azure Automation runbook to get Azure to work for you.
Honorary Scripting Guy, Sean Kearney, is here to show you the most importance piece of Azure Automation: the actual scheduling of runbooks to allow you to let Azure do all the automation for you! This is the final post in a five-part series. To catch up, read:
- Using Azure Automation: Part 1
- Using Azure Automation: Part 2
- Using Azure Automation: Part 3
- Using Azure Automation: Part 4
Yesterday, we finished our script to power down the virtual machines in Microsoft Azure, including sending some output to indicate the status of the shutdown.
If we go back into our runbook, things look a little different now. Instead of defaulting to the draft editing window, we are in the published runbook window as shown here:
Clicking Start will trigger the runbook just as if you were at a Windows PowerShell command prompt running a script.
You will see a status bar that indicates that the job is running. You can check the status of the job by selecting the View Job option on the status bar in Azure. Clicking this will open a status window that shows you the ongoing results of the job:
Drilling down further (near the bottom), we can view the Output. This window will show us any output that would normally echo to a Windows PowerShell console, including the data we specified with the Write-Output cmdlet.
If you look at the upper right of the screen, you'll see a History tab, which allows you to view a log of the working runbooks.
But now for the fun stuff...setting this as scheduled task. As shown here, to the right of Author, you'll see a tab marked Schedule. Click that to begin creating a schedule.
With a new Azure Automation instance, you'll have a blank schedule as you see in the following image. It has two simple options: Link to a New Schedule or Link to an Existing Schedule.
Click Link to a New Schedule to create a new schedule for your Azure Automation runbook.>>5
The first part will require a name and a description for the scheduled task. Again lets be sensible. Naming the scheduled task "Marvin the Paranoid Android" might be funny, but it won't make a lick of sense later (unless your description is really good—but even then, I wouldn't suggest that).
In the following example, we call this scheduled task EndOfDayShutdownTask:
If we click our friendly neighborhood checkmark to continue, we'll see that we have three basic scheduling options:
- One time: Do this once and only once.
- Hourly: Repeat on a scheduled hourly cycle (minimum every hour, but you can increase the gap).
- Daily: Repeat this on a scheduled daily cycle (minimum every day, but you can increase the gap).
With the daily and hourly choices, you can also provide a date that this should stop running. In our example, this task will occur every day (Saturdays and Sundays included) at 5 P.M.:
We'll click that lovely little checkmark at the bottom of the wizard. Our schedule is created, and we can see our runbook set up as an available job:
As shown here, you'll see three for options for your scheduled runbook. They are Link, View Details, and Unlink:
Link is the same wizard you previously saw on the screen. Unlink allows you to remove that runbook as a scheduled task.
Now here is where you can schedule a runbook the fun way. If you'd like to use Windows PowerShell to do exactly what we just did, you can! In the Azure module, there is a cmdlet called New-AzureAutomationSchedule. We could use it do define our daily schedule like so:
$Description='Daily Task to occur at 5pm EST to shut down my Azure VMs'
New-AzureAutomationSchedule –AutomationAccountName $AzureAutomationAccount –Name $TaskName –Starttime $StartTime –DailyInterval $DailyFrequency –Description $Description
There is, of course, far more that you can do. This series has given you a quick entry level introduction to Azure Automation. If nothing else, it's a great way to keep costs under control when people forget to shut things down at the end of the day!
I invite you to follow The Scripting Guys on Twitter and Facebook. If you have any questions, send an email to The Scripting Guys at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then remember eat your cmdlets every day with a taste of creativity.
Sean Kearney, Windows PowerShell MVP and Honorary Scripting Guy