I'm trying to get the list of DL groups user is member of in O365. I'm using below PS Script but I'm getting the error message.
$user = "[email protected]"
$dlGroups =Get-DistributionGroup | Get-DistributionGroupMember | Where-Object {$_.PrimarySmtpAddress -eq $userEmail}
foreach($dl in $dlGroups){
Write-Output $dl
#Remove-DistributionGroupMember -Identity $dl.Identity -Member $user
}
error:
Write-ErrorMessage : ||The operation couldn't be performed because 'Bus Training School' matches multiple entries.
I'm trying to get the list of DL groups user is member of in O365. I'm using below PS Script but I'm getting the error message.
$user = "[email protected]"
$dlGroups =Get-DistributionGroup | Get-DistributionGroupMember | Where-Object {$_.PrimarySmtpAddress -eq $userEmail}
foreach($dl in $dlGroups){
Write-Output $dl
#Remove-DistributionGroupMember -Identity $dl.Identity -Member $user
}
error:
Write-ErrorMessage : ||The operation couldn't be performed because 'Bus Training School' matches multiple entries.
Share
Improve this question
edited Feb 3 at 19:18
Chandra Mohan
asked Feb 3 at 18:30
Chandra MohanChandra Mohan
11 bronze badge
2 Answers
Reset to default 0Unsure why you get this error, possibly by looking at the error, the property value piped into Get-DistributionGroupMember
results into an ambiguous result.
However, an easier and more efficient way to approach the problem is to use the -Filter
. According to the Filterable properties for the Filter parameter on Exchange cmdlets documentation, Members is a filterable attribute as long as you supply the user's DinstinguishedName
or CanonicalName
. If you're not sure how to get the user's DistinguishedName
, you can use Get-User
or Get-Recipient
.
$userDN = 'CN=test1,...,DC=myDomain,DC=com'
$dlGroups = Get-DistributionGroup -Filter "Members -eq '$userDN'"
Your first line contains an unknown variable, $userEmail
. Next, Get-DistributionGroupMember
appears to be binding DisplayName
from the groups, which is not guaranteed to be unique. Here are the unique properties you can pass instead
- Name
- Alias
- Distinguished name (DN)
- Canonical DN
- Email address
- GUID
That aside, even if your command worked, you would not end up with a list of distribution groups, you would end up with a list of distributiongroup members. I recommend breaking your command up by first getting the distribution groups, then you can do something like add the members as another property of the groups, then finally you can process against that list.
$user = "[email protected]"
$dllist = Get-DistributionGroup
$dllist = $dllist | Select-Object *,@{n='Members';e={$_.distinguishedname | Get-DistributionGroupMember}}
foreach($dl in $dllist){
if(@($dl.members.PrimarySmtpAddress) -contains $user){
"$($dl.Name) contains $user"
}
}
Note that I surrounded the potential members primarysmtpaddress in @()
so that even if the list contains one member it will be treated as an array. You may also want to check if there are any members first and skip those that are empty.