Home Mass creating automation account schedules
Post
Cancel

Mass creating automation account schedules

Automation accounts in Azure are great, they let you run any arbitrary powershell code in the cloud.
But they have a few frustrating aspects, one of which is the tediousness of creating schedules. You have to do it for each new Automation Account and the UI doesn’t have any sort of templating or copy paste options. So I sat down today for 10 minutes to solve this frustration for myself.
My requirement is daily schedules, one for each half hour of the day. Your needs may vary but the code should give you an idea of the path to take.

Desktop View Me when I finished this code.

The code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#$selectedSubscription = Get-AzSubscription | Out-GridView -Title "sub" -OutputMode Single
$selectedResourceGroup = Get-AzResourceGroup | Out-GridView -Title "Select Resource Group" -OutputMode Single
$selectedAutomationAccount = Get-AzAutomationAccount -ResourceGroupName $selectedResourceGroup.ResourceGroupName | Out-GridView -Title "Select Automation Account" -OutputMode Single
$selectedTimeZone = Get-TimeZone -ListAvailable | Out-GridView -Title "Select TimeZone" -OutputMode Single

# Loop through each half-hour interval of the day
for ($hour = 0; $hour -lt 24; $hour++) {
    for ($minute = 0; $minute -le 30; $minute += 30) {
        # Format the hour and minute to ensure they are two digits
        $formattedHour = "{0:D2}" -f $hour
        $formattedMinute = "{0:D2}" -f $minute

        # Calculate the start time for each half-hour interval using the current date
        $startTime = (Get-Date -Hour $hour -Minute $minute).AddDays(1)

        # Create a splat object for each schedule with the formatted time in the name
        $splat = @{
            Name = "Everyday ${formattedHour}${formattedMinute}"
            AutomationAccountName = $selectedAutomationAccount.AutomationAccountName
            ResourceGroupName = $selectedResourceGroup.ResourceGroupName
            TimeZone = $selectedTimeZone.id
            StartTime = $startTime
            DayInterval = 1
        }

        # Create the new schedule
        New-AzAutomationSchedule @splat
    }
}

Happy scheduling!

This post is licensed under CC BY 4.0 by the author.