Aug 26 2022 07:31 AM - edited Aug 26 2022 07:37 AM
Hi all,
my company have several domains under its tenant.
Now I should get, for each domain under the tenant, the list of the members of every dsitribution list.
if too hard, for me would be fine to get the members of a distribution list of a given domain
do you also know how I can choose the output values? I mean, mailbox or other info of the members
thanks!
Aug 27 2022 01:58 AM - edited Aug 27 2022 01:59 AM
If it's just a case of multiple domains within a single tenant, you don't have to do anything special for this. It would be a bit more involved if you had to do this across multiple tenants.
Here's a basic template to get you started that uses the Microsoft ExchangeOnline V2 module.
Note: I've deliberately specified a limit using "ResultSize 10" on line 1 as this isn't a quick process, and starting off with "ResultSize unlimited" may not be wise. But feel free to change the value as you see fit.
Get-EXORecipient -RecipientType MailUniversalDistributionGroup, DynamicDistributionGroup -Properties ExchangeGuid, ExternalDirectoryObjectId -ResultSize 10 |
ForEach-Object {
$Group = $_;
# Set up the common attributes now so that we're not doing it multiple times (per group type) later.
$Output = @{
Id = $Group.ExchangeGuid;
AadId = $Group.ExternalDirectoryObjectId;
GroupType = $Group.RecipientType;
GroupName = $Group.Name;
GroupPrimarySmtpAddress = $Group.PrimarySmtpAddress;
GroupEmailAddresses = $Group.EmailAddresses;
Members = [System.Collections.Generic.List[PSCustomObject]]::new();
}
switch ($Group.RecipientType)
{
"MailUniversalDistributionGroup" {
$null = $Output.Members.Clear();
Get-DistributionGroupMember -Identity ($Group.Identity) |
ForEach-Object {
$null = $Output.Members.Add([PSCustomObject] @{
MemberId = $_.ExchangeGuid;
MemberAadId = $_.ExternalDirectoryObjectId;
MemberType = $_.RecipientType;
MemberAddress = $_.PrimarySmtpAddress;
});
}
[PSCustomObject] $Output;
break;
}
"DynamicDistributionGroup" {
$null = $Output.Members.Clear();
Get-DynamicDistributionGroupMember -Identity ($Group.Identity) |
ForEach-Object {
$null = $Output.Members.Add([PSCustomObject] @{
MemberId = $_.ExchangeGuid;
MemberAadId = $_.ExternalDirectoryObjectId;
MemberType = $_.RecipientType;
MemberAddress = $_.PrimarySmtpAddress;
});
}
[PSCustomObject] $Output;
break;
}
}
}
Cheers,
Lain
Aug 27 2022 02:12 AM
SolutionI should have added this before posting above.
If you wanted to flatten the output into a very basic CSV file rather than do further work with the objects, you can do it like this:
.\Get-ExoDistributionListMembers.ps1 |
ForEach-Object {
$Group = $_;
$Group.Members |
ForEach-Object {
$_ | Select-Object -Property @{n="GroupType"; e={ $Group.GroupType}}, @{n="GroupName"; e={ $Group.GroupName}}, MemberType, MemberAddress;
}
} | Export-Csv -Path ".\distributionLists.csv" -NoTypeInformation;
Cheers,
Lain
Aug 29 2022 12:00 AM
Aug 27 2022 02:12 AM
SolutionI should have added this before posting above.
If you wanted to flatten the output into a very basic CSV file rather than do further work with the objects, you can do it like this:
.\Get-ExoDistributionListMembers.ps1 |
ForEach-Object {
$Group = $_;
$Group.Members |
ForEach-Object {
$_ | Select-Object -Property @{n="GroupType"; e={ $Group.GroupType}}, @{n="GroupName"; e={ $Group.GroupName}}, MemberType, MemberAddress;
}
} | Export-Csv -Path ".\distributionLists.csv" -NoTypeInformation;
Cheers,
Lain