Menu Close

Script: Create linked runbookschedules with Powershell

This one will be short and simple.

I have been working on a project where i wanted to give people permissions to Azure resources for a limited amount of time.
To do this, I created a runbook that could add or remove permissions. Next to that, I needed a runbook that could create and link a schedule to the permission-runbook, with the right parameters of course.

I needed two different cmdlets from the AzureRM.Automation-module:

New-AzureRmAutomationSchedule

to create the schedule.

Register-AzureRmAutomationScheduledRunbook

to link the schedule to the runbook.

The code isn’t that complicated, but I did it so often within these scripts, it made sense to create a little function.
The function also provides a solution to a problem: the schedulename has to be unique. This isn’t a problem when creating it manual, but I wanted these scripts to run automated.

What the function does:
– It creates a unique name for the schedule by combining a string and a number.
– It creates a onetime schedule
– It links the schedule to the runbook.

To have a learning experience, I invested some time in setting up -WhatIf, something that I haven’t practiced enough till now. I found a great blogpost by Ben Thomas that explained it very clearly, I recommended it if you haven’t used WhatIf in a function before

Here it is:

So how does that look?

And there we go, the schedule has been created.

As a bonus, this little script will remove all expired schedules that will now clutter up your automation account:


1 Comment

  1. Pingback:Using Logic Apps: Schedule a Runbook with an Office365 Calendar Appointment – 4bes.nl

Leave a Reply

Your email address will not be published. Required fields are marked *