How to use Exchange Online PowerShell on Azure Functions with Managed Identity
Last updated on August 12, 2024
In this article, we’ll connect to Exchange Online in a PowerShell script run by an Azure Function. The script will use the Function App’s managed identity for authenticating to and executing the commands on Exchange Online.
Table of Contents
- Enable the Function App’s Managed Identity
- Grant the required permissions
- Specify the required modules to load
- Setup the Function App configurations
- Add the script to the function app
Enable the Function App’s Managed Identity
Without further ado, let’s dive into the nitty-gritty.
- Open the Identity blade
- Change the system assigned managed identity Status to On.
- Click on Save and Yes when prompted
- Copy the Object ID that appears upon saving to, e.g., Notepad. You’ll need this later.
Grant the required permissions
We need to grant the Function App’s managed identity Exchange Administrator permissions. The permissions can be granted with the script below. Before executing the script, you need to do the following:
- Install the Microsoft Graph SDK if you have not done so before. That happens by executing the following PowerShell command as an administrator:
Install-Module Microsoft.Graph -Scope CurrentUser
- Set your tenant ID and the managed identity object ID you copied earlier as the script variable values.
Specify the required modules to load
We need to install the ExchangeOnlineManagement PowerShell module to our Azure Function App. That happens through the following steps.
- Open the Function App App files blade.
- Select requirements.psd1 from the dropdown.
- Because we are using the Azure Function App managed identity for authentication, we need to install the required Az.Accounts and Az.ManagedServiceIdentity modules. I recommend installing these two Azure PowerShell modules (and other possible Az modules required by your script) explicitly instead of installing the entire Az module collection because the latter takes a really long time and results in a lot of hassle when it comes to installing the dependencies for the first time (namely, the function timeouts on a consumption plan).
- Below the uncommented Az module, add references for the latest versions of the ExchangeOnlineManagement, PowerShellGet, PackageManagement modules. The file should now look something like the one below.
- Remember to Save the changes.
Setup the Function App configurations
On your function app in Azure Portal, click on Configuration and add the following application setting.
Name | Value | Description |
---|---|---|
Tenant | yourtenant.onmicrosoft.com | The prefix is the same as in your SharePoint URL. |
Don’t forget to save your changes!
Add the script to the function app
Now, it is time to add a PowerShell script to our function that will authenticate to Exchange Online using the Function App managed identity.
- Open the Overview blade.
- Create a new function:
- Leave Development environment to its default value (Develop in portal).
- Select the type of trigger you want to use.
- Give your function a descriptive name.
- Configure the rest of the available settings (depending on the selected trigger type).
- Click on Create.
- Click on Code + test in the navigation.
- Finally, add the following lines in your PowerShell script to implement authentication using the managed identity, and click on Save.
Afterword
I write these blog posts just as much for my own benefit as yours. Now, we have an article we can quickly browse through whenever we need to implement an Azure Function that authenticates to Exchange Online with a managed identity.
I hope you, my dear reader, also found this article helpful and got your function working. Happy coding, and until next time!
Laura
Hi Laura
Tahnks for this very nice tutorial.
I’m totally new to Azure functions and trying to follow your steps.
However i’m stuck while running :
$roleDefinitionId = (Get-MgRoleManagementDirectoryRoleDefinition -Filter “DisplayName eq ‘Exchange Administrator'”).Id
i’m getting this error :
Get-MgRoleManagementDirectoryRoleDefinition_List: Method not found: ‘System.Net.Http.HttpClient Microsoft.Graph.PowerShell.Authentication.Helpers.HttpHelpers.GetGraphHttpClient(Microsoft.Graph.PowerShell.Authentication.IAuthContext)’.
I’ve tried to debug with no luck.
Uninstalled Microsoft Graph modules and reinstalled the latest (2.24.0) but still got the same error.
Have you faced this ? Any idea how to fix it ?
Thanks for your help
Samuel