Problems with connecting to Exchange Online from azure automation using managed identity.

Oleg Tserkovnyuk 666 Reputation points
2022-12-20T10:36:55.407+00:00

Hello,

I would like to run from Azure Automation Get-UnifiedGroup.
To do this I use code:

Connect-ExchangeOnline -ManagedIdentity -Organization atlastechnicalab.onmicrosoft.com  
Get-UnifiedGroup  

Unfortunately Connect-ExchangeOnline crashes with error ‘UnAuthorized’.

To grant access to managed identity used by my Azure Automation I used code:

Connect-MgGraph -Scopes RoleManagement.ReadWrite.Directory  
$RoleID = (Get-MgRoleManagementDirectoryRoleDefinition -Filter "DisplayName eq 'Exchange Administrator'").Id  
#$PrincipalId I take from my automation > identity > system assigned > object (principal) ID   
New-MgRoleManagementDirectoryRoleAssignment -PrincipalId $PrincipalId -RoleDefinitionId $RoleID -DirectoryScopeId "/"  

I followed this guide to grant permissions and this guide to connect to Exchange Online.

My runbook based on PS runtime v7.1 and use module ‘ExchangeOnlineManagement‘ 3.0.0 for runtime 7.1.

Also, would be very thankful if someone can help to figure out minimal permissions, I need to grant to my Azure Automation to connect to Exchange Online and run command Get-UnifiedGroup.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,311 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,798 questions
0 comments No comments
{count} vote

Accepted answer
  1. Vasil Michev 114K Reputation points MVP
    2022-12-20T10:54:49.89+00:00

    Did you add the Exchange.ManageAsApp permission in addition to the Azure AD role? Without it, you will get the UnAuthorized error.

    Btw, there is a known issue with the *-UnifiedGroup cmdlets, you will not be able to run those via this method. So if all you want to do is manage M365 groups, best use the Graph API instead.


1 additional answer

Sort by: Most helpful
  1. John Bruijntjes 0 Reputation points
    2025-02-11T13:39:54.27+00:00

    Make sure to use the object (principal) id and not the clientID of the managed identityUser's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.