Copy a Distribution Group to an M365 Group using Power Automate

 


I was asked if it was possible to synchronize an Exchange Online Distribution Group members to an M365 Group. There's nothing that natively does this, but I'm going to show you how i used Power Automate to sync members from one to the other.

Before you start, make sure the welcome emails are turned off in the receiving M365 group, so that each time you wipe the group and add members to it, your users aren't getting spammed with emails.

#Parameters
$GroupId = "hrteam@CrescentIntranet.com"
 
#Connect to Exchange Online
Connect-ExchangeOnline -Credential (Get-Credential)
 
#Disable the Group Welcome Message Email
Set-UnifiedGroup -Identity $GroupId -UnifiedGroupWelcomeMessageEnabled:$false

Now, let's create the flow:
  1. Start with a blank Flow, and make the trigger be a Recurrence trigger. Make it run as often as you want to sync.
  2. Create two string variables. 
    1. One to hold the Object ID of the Distribution Group (varDistroGroupID)
    2. the other to hold the Object ID of the M365 Group (varM365GroupID). 
  3. Then from the Office 365 Groups connectors, add a "Send an HTTP request V2". 
    1. URI: https://graph.microsoft.com/v1.0/groups/@{variables('varDistroGroupID')}/transitiveMembers?$top=999
    2. Method: GET
    3. Content-Type: application/json
  4. Add a Filter Array next
    1. From: body('Send_an_HTTP_request_V2')['value']
    2. item()?['@.odata.type'] is equal to #microsoft.graph.user
  5. Add a Parse JSON next
    1. Content: body('Filter_array')
    2. Schema:
      1. {
            "type""array",
            "items": {
                "type""object",
                "properties": {
                    "@@odata.type": {
                        "type""string"
                    },
                    "id": {
                        "type""string"
                    },
                    "displayName": {
                        "type""string"
                    },
                    "mail": {
                        "type""string"
                    },
                    "userPrincipalName": {
                        "type""string"
                    }
                },
                "required": [
                    "@@odata.type",
                    "id",
                    "displayName",
                    "mail",
                    "userPrincipalName"
                ]
            }
        }
  6. Add a List Group Members and for the ID use varM365GroupID
  7. Next we are going to remove all members of the M365 group
    1. From the Office 365 Groups connectors, add the Remove Member from Group. Use varM365groupID, and for the User Principal Name use UPN you got from the previous step.
  8. Then you're going to add the members of the distribution group to the M365 group by using a "Add Member to Group" action from the Office 365 Groups connectors.
    1. Group Id: varM365GroupID
    2. User Principal Name: userPrincipalName from the Parse JSON step.
I recommend you test this on a testing M365 group, because you don't want to erase your prod data before you know if it works correctly!




Sources:

Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.