In this blog post, we will discuss how to connect to Exchange Online using Azure Automation.
Exchange Online is a cloud-based email and calendaring service provided by Microsoft as a part of Office 365. Azure Automation is a cloud-based automation service provided by Microsoft.
The primary use case in using Azure Automation to connect to Exchange Online is when we need to automate a specific task in Exchange Online.
Before we begin, make sure you have the following:
- Microsoft 365 Global Administrator account without MFA enabled – This account will be used to connect
- Azure Automation runbook – Azure automation runbook running PowerShell 5.1
- Azure Automation credentials – Add credentials to the automation account that contains the Microsoft 365 Global Administrator username and password. Take note of the name you give the credential.
- ExchangeOnlineManagement module – Install the ExchangeOnlineManagement module in the automation account.
In our case, the creds are called M365Admin
Once the creds are saved, create or open a PowerShell 5.1 runbook and add the following code.
The code below will use the M365 global admin account to connect to Exchange and get a list of all the mailboxes in the tenant.
$credObject = Get-AutomationPSCredential -Name "M365Admin" Connect-exchangeonline -Credential $credObject Get-EXOCasMailbox |ft
We can test our connection to Exchange Online. In the Automation account, click on the “Test Pane” button in the runbook editor.
Publish and Schedule
Once you have confirmed that the runbook is working correctly, you can publish it and schedule it to run on a regular basis. In the runbook editor, click on the “Publish” button, and then select “Yes” when prompted to confirm.
Next, click on the “Schedules” section and then click on “Add a schedule.” Give your schedule a name, select the frequency and start time for the schedule, and then select the runbook you just published.
Leave a Reply