An Azure service that is used to automate, configure, and install updates across hybrid environments.
Hello Tyler Johnson, you are running an Azure Automation PowerShell runbook and The runbook uses Exchange Online PowerShell command Connect-ExchangeOnline -ManagedIdentity.
The goal is to add users to a Distribution Group using a CSV file, The runbook fails with: Failed UnAuthorized (UnAuthorized / 401).
Managed Identity authentication works differently from user login. When you use Connect-ExchangeOnline -ManagedIdentity, you are NOT authenticating as your admin user, Global admin or Exchange admin. But You are authenticating as The Azure Automation Account's Managed Identity (Service Principal). Exchange Online does not automatically trust this identity. You must explicitly grant it Exchange permissions. Because of this you are facing error Unauthorized.
The recommendation is to user PowerShell version 7.1 or onwards Azure Automation Runbook Types | Microsoft Learn
To resolve this please follow the below steps. thanks
Use Azure managed identities to connect to Exchange Online PowerShell | Microsoft Learn
Step 1: Enable Managed Identity for the Automation Account
- Go to Azure Portal > Automation Account > Identity
- Turn System Assigned > Set to “ON”> Click Save
- Copy the Object (Principal) ID
Step 2: Assign Azure AD Role:
- Go to Azure Portal > Microsoft Entra ID > Roles and Administrators
- Assign one of Exchange Administrator (Recommended) OR Global Administrator (for testing only)
- Add the Automation Account Managed Identity.
Assign Microsoft Entra roles - Microsoft Entra ID | Microsoft Learn
Step 3: Assign Exchange RBAC Role:
Now connect to Exchange Online manually from your admin machine:
Connect-ExchangeOnline
Find the Managed Identity service principal:
Get-ServicePrincipal | Where-Object {$_.DisplayName -like "*YourAutomationAccountName*"}
Then assign role:
For adding distribution group members, Recipient Management is sufficient:
Add-RoleGroupMember -Identity "Recipient Management" -Member "<ManagedIdentityDisplayName>"
Manage role groups in Exchange Online | Microsoft Learn
Step 4: Grant the Exchange.ManageAsApp API permission for the managed identity to call Exchange Online:
Follow the this document to grant the access Use Azure managed identities to connect to Exchange Online PowerShell | Microsoft Learn
Step 5: Assign Microsoft Entra roles to the managed identity:
Use Azure managed identities to connect to Exchange Online PowerShell | Microsoft Learn
Step 6: Assign Azure AD Role or API Permission
- Go Azure portal > Select Microsoft Entra ID (Azure Active Directory).
- Click Enterprise Applications (not App Registrations).
- At the top, change Application Type filter to All Applications.
- Search for the name of your Automation Account > click on your automation account
- Select Permissions on left side panel
- Click Add API Permission > Office 365 Exchange Online > Application Permissions > select Exchange.ManageAsApp
- Click Grant Admin Consent
- This ensures the Managed Identity is authorized to connect to Exchange Online.
Step 7: when you are connecting you Exchange model please validate your primary domain
- Go Azure portal > Select Microsoft Entra ID (Azure Active Directory).
- Overview > check primary domain
- This primary domain you have to user when you are connecting to Exchange online using managed identity.
Connect-ExchangeOnline -ManagedIdentity -Organization "<Your primary domain>"
For adding distribution group members, Fix Your Script instead $memberList = "Import-Csv C:\Test\JohnsonTest.csv" you can update this to correct version $memberList = Import-Csv "C:\Test\JohnsonTest.csv"
Note: On your local machine, C:\Test\JohnsonTest.csv exists, so Import-Csv works normally, But in the Azure Automation cloud sandbox, you cannot use local file paths like C:\Test\JohnsonTest.csv. You need a cloud-friendly alternative.
Hope this helps. and please feel free to reach out if you have any further questions. Thanks