Hello everyone, cycling through members of a distribution list and comparing them to the members of another group is a relatively simple thing to solve in PowerShell but took me some time to wrap my head around in Power Automate.
Below, I broke the process down into multiple steps. The end goal of this project is to be able to detect new members of a distribution list and perform specific actions for those new users such as adding them to a team, send them a custom email or so many other possibilities.
In the use case below, my objective is to analyze the members of a distribution list which is populated by an HR process and automatically add new members to a specific Team, and send them a custom welcoming email with details.
Create the flow- Go to My flows and Click New:
- I went with a Scheduled flow so that this process is completely hands-off.
- Name your flow and your schedule, this can be modified later so specify a date way off in the future, this will give you enough time to test it out.
|
|
Get the distribution list [DL] members- Add an action and type “Azure AD” in the search box. Select Azure AD and scroll down in the actions section to “Get group Members”. This will be the action that gets the Distribution List members.
- Now you will need to enter your Group ID (Object ID), this is your distribution lists unique identifier. You can find this identifier in the Azure AD Admin Center under the group object. Or by using PowerShell and searching for the group:
I suggest renaming the action to something more obvious like “Get Distribution List Members” instead of the default “Get group members”. This will help with the flow later.
| |
Initialize a variable to store the DL members- Next, you’ll need a variable to trim down the output of the “Get Distribution List Members” action:
- Create a new step and type “Initialize Variable”.
- Specify the name of the variable (remember, rename it something obvious) in this case we’ll name it “DLMembers”.
- Select “Type” and specify “Array”.
- See the screenshot to the right for the flow as it should look up until this point.
| |
Append the DL members to the variable- Now we need to populate the variable “DLMembers” with the User ID of each member of the distribution list. To do this we’ll cycle through each member in the output of “Get Distribution List Members” and inject the “User ID” value into the “DLMembers” variable.
- Create a new step and select “Control” and “Apply to each”.
- Under “Select an output from previous steps” select “add a dynamic value”.
- Under “Get Distribution List Members” or whatever you named that first step, select “Group Members”.
- Now click “Add an action”, search for “Append to array variable” and select “Append to array variable”.
- Under name select “DLMembers” and value select “add a dynamic value”. Under “Get Distribution List Members” select “Group Members Id”.
- This would be a good time to click “Save”
Important note: This action will list all distribution list members. This includes all nested groups. To detect a nested group, you will need additional steps. These will be outlined in a future post. Note: Again, as suggested previously it would be easier if you rename the actions above. See screenshot below, for example. I renamed “Apply to each” to “Foreach DL Member append ID to DLMembers” and “Append to array variable” to “Append to array variable DLMembers”.
| |
Get the current Team or O365 Group members- The next step will get a list of all the members of the specified Team or Office 365 Group.
- Create a new step and type “Office 365 Groups” and “List group members”.
- For “Group ID” specify the Team or O365 Group you want to compare the DL Members to.
- Click “Show advanced options” and set the “Top” to 999 (which is the maximum)
Note: Now, keep in mind some of the limitations for Teams or O365 Groups. There can only be a maximum of 10,000 members in a Team. https://aka.ms/AAactar This function allows only listing a maximum of 999 members.
| |
Add a new variable for Team Members- Create a new variable to store the Team Members.
- Specify the name of the variable (remember, rename it something obvious) in this case we’ll name it “TeamMembers”.
- Select “Type” and specify “Array”.
- See the screenshot for the flow as it should look up until this point.
| |
Append the Team members to the variable- Now we need to populate the variable “TeamMembers” with the User ID of each member in the Team. To do this we’ll cycle through each member in the output of “Get Team Members” and inject the “User ID” value into the “TeamMembers” variable.
- Create a new step and select “Control” and “Apply to each”.
- Under “Select an output from previous steps” select “add a dynamic value”.
- Under “Get Team Members” or whatever you named that first step, select “Value”.
- Now click “Add an action”, search for “Append to array variable” and select “Append to array variable”.
- Under name select “TeamMembers” and value select “add a dynamic value”. Under “Get Team Members” select “User Id”.
- This would be a good time to click “Save”
| |
Run the comparison and do stuff- Now we need to compare the DLMember variable to the TeamMember variable. We need to detect if a User Id in the DLMember variable already exists in the TeamMember variable. The trick in this type of loop is the “Current item” dynamic value.
- Create a new step and add a new “Control” and select “Apply to each”. Rename this loop to “Foreach DLMember check if already in Team”.
- The “Select an output from previous steps” add dynamic value and under variable select “DLMembers”.
- Click “Add and action” and select “Control” and “Condition”.
- “Add dynamic content” under Variables select “TeamMembers” and select “contains” and then select “Add dynamic content” and under “Foreach DLMember check if already in Team” select “Current Item”
| |
Here is an image of the entire flow:
I hope this proves useful.
Brian Baldock