最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - Get DL Groups user is member of in Exchange online - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 0

Unsure 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.

发布评论

评论列表(0)

  1. 暂无评论