Primer: How to Schedule Azure Automation Runbooks to Process Microsoft 365 Data
Let Azure Automation Execute Runbooks Using an Automation Schedule
After covering the basics of using Azure Automation runbooks to access Microsoft 365 data, the second part of the primer covers how to write data generated by a runbook to a SharePoint Online list. Populating data on an on-demand basis is valuable, but consistent gathering of data for comparison purposes can be so much more valuable. For that to happen, we need Azure Automation to execute the runbook on a schedule.
Runbook Details
The runbook that I have been using is called GetRecentAccounts. Figure 1 shows the runbook properties as viewed through the Azure portal. Essential details here are the name of the automation account associated with the runbook (M365Automation) and the resource group it uses (ExoAutomation).

Resource groups are containers that Azure uses to hold resources. Resource groups are also used to track consumption, such as the processing performed by services such as Microsoft 365 backup or SharePoint document translation. An automation schedule is a resource, so it’s managed in a resource group. You might be familiar with the Windows Task Scheduler. The automation schedule is a more secure and better solution for running PowerShell scripts.
Creating an Automation Schedule
Two methods exist to create an automation schedule: through the GUI of the Azure portal or PowerShell using cmdlets from the Az.Accounts and Az.Automation modules. Because this discussion is all about PowerShell, we’ll take the latter option. Besides, using PowerShell to manage object often exposes details that are otherwise easily overlooked.
After installing the latest version of the modules from the PowerShell gallery, connect to the Azure account like this:
Connect-AzAccount -TenantId $TenantId -SubscriptionId $SubscriptionId -AccountId Tony.Redmond@office365itpros.com
You’ll need to know identifier for the tenant and Azure subscription and the account you sign in with must be entitled to manage Azure resources. The subscription identifier is listed in the runbook details (Figure 1) and the tenant identifier is easily found by running the Get-MgOrganization cmdlet:
(Get-MgOrganization).Id
After establishing a connection, you can manage the Azure automation resources available to the account. This code shows how to create a new automation schedule to run at 17:30 every weekday. The names of the automation account and resource group are required. This automation schedule doesn’t have an end date. If you want to add an end date, pass a valid date value in the EndTime parameter. More details about creating automation schedules are in the documentation.
# Set up when the new schedule will start (today at 17:30) $StartTime = (Get-Date "17:30:00") $ResourceGroup = "ExoAutomation" $ScheduleName = "WeekDaySchedule" $AutomationAccountName = "M365Automation" # Define what days of the week the schedule will run [System.DayOfWeek[]]$WeekDays = @([System.DayOfWeek]::Monday..[System.DayOfWeek]::Friday) # Create the new schedule New-AzAutomationSchedule -AutomationAccountName $AutomationAccountName -Name $ScheduleName -StartTime $StartTime -WeekInterval 1 -DaysOfWeek $WeekDays -ResourceGroupName $ResourceGroup -Description 'Schedule to execute runbooks at 17:30 UTC on weekdays' # Check its details Get-AzAutomationSchedule -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccountName StartTime : 22/01/2025 17:30:00 +00:00 ExpiryTime : 31/12/9999 23:59:00 +00:00 IsEnabled : True NextRun : 22/01/2025 17:30:00 +00:00 Interval : 1 Frequency : Week MonthlyScheduleOptions : WeeklyScheduleOptions : Microsoft.Azure.Commands.Automation.Model.WeeklyScheduleOptions TimeZone : Etc/UTC ResourceGroupName : ExoAutomation AutomationAccountName : M365Automation Name : WeekDaySchedule CreationTime : 22/01/2025 17:12:50 +00:00 LastModifiedTime : 22/01/2025 17:16:02 +00:00 Description : Schedule to execute runbooks at 17:30 UTC on weekdays
Register the Runbook with an Automation Schedule
Before Azure Automation can execute a runbook without human intervention, the runbook must be registered with an automation schedule. This code does the job, as does the Link to schedule option shown in Figure 1.
# Name of runbook to schedule $RunbookName = "GetRecentAccounts" Register-AzAutomationScheduledRunbook -AutomationAccountName $AutomationAccountName -Name $RunbookName -ScheduleName $ScheduleName -ResourceGroupName $ResourceGroup Register-AzAutomationScheduledRunbook: The runbook has no published version. Runbook name GetRecentAccounts.
Or rather, it would if the runbook was published. Up to now, the runbook has been under active development, and we’ve used the test pane feature to make sure that the runbook code runs as expected. Eventually, a runbook reaches the point where the code works and is stable. At that point, you should publish the runbook to create a version that Azure Automation can schedule. Development can still continue, but Azure Automation will only run the published version.
To create a published version, edit the runbook and choose the Publish option (Figure 2).

Alternatively, use the Publish-AzAutomationRunbook cmdlet to publish the runbook:
$Params = @{} $Params.Add("Name", $RunbookName) $Params.Add("ResourceGroupName", $ResourceGroup) $Params.Add("AutomationAccountName", $AutomationAccountName) Publish-AzAutomationRunbook @Params Location : westeurope Tags : {} JobCount : 0 RunbookType : PowerShell72 Parameters : {} LogVerbose : False LogProgress : False LastModifiedBy : State : Published ResourceGroupName : ExoAutomation AutomationAccountName : M365Automation Name : GetRecentAccounts CreationTime : 20/01/2025 15:03:41 +00:00 LastModifiedTime : 22/01/2025 18:06:47 +00:00 Description : Find recently created accounts
Either way, the runbook can now be registered with an automation schedule in the Azure portal or by running the Register-AzAutomationScheduledRunbook cmdlet. Confirmation of the scheduling can be obtained by examining the details of the schedule in the Azure portal (Figure 3) or by running the Get-AzAutomationScheduledRunbook cmdlet:
Get-AzAutomationScheduledRunbook -RunbookName $RunbookName -ScheduleName $ScheduleName -ResourceGroupName $ResourceGroup -AutomationAccountName $AutomationAccountName ResourceGroupName : ExoAutomation RunOn : AutomationAccountName : M365Automation JobScheduleId : 9a2aadd0-c3ac-4baa-8cfb-855b12a1a277 RunbookName : GetRecentAccounts ScheduleName : WeekDaySchedule

To confirm that everything’s running as expected, wait until after the job should run and check the output location for new data or use the Get-AzAutomationJob cmdlet:
Get-AzAutomationJob -ResourceGroupName $ResourceGroup -RunbookName $RunbookName -AutomationAccountName $AutomationAccountName | Where-Object {$_.Status -eq 'Completed'} | Format-Table StartTime, EndTime, RunbookName, Status StartTime EndTime RunbookName Status --------- ------- ----------- ------ 22/01/2025 17:30:59 +00:00 22/01/2025 17:31:53 +00:00 GetRecentAccounts Completed
The Value of Scheduled Runbooks
There’s nothing like an automated schedule to make sure that jobs get run at the right time. The combination of runbooks and schedules means that you can be sure that important Microsoft 365 automation processes are executed. That’s a nice feeling.
Need some assistance to write and manage PowerShell scripts for Microsoft 365? Get a copy of the Automating Microsoft 365 with PowerShell eBook, available standalone or as part of the Office 365 for IT Pros eBook bundle.