powershell - How to Compare Sets of Users - Stack Overflow

I am trying to query a domain for all users from an OU, in a specific group, and not in another specifi

I am trying to query a domain for all users from an OU, in a specific group, and not in another specific group.

First, I query Active Directory for all three sets of users: the OU, group to include, and group to exclude. The group to exclude happens to be a distribution group, so I had to query for it a different way.

$OU = Get-ADUser -Filter * -SearchBase "OU=Developers,OU=Everyone,DC=Organization,DC=com" -Properties MemberOf
$include = Get-ADGroupMember -Identity "this group"
$exclude = Get-ADObject -Filter 'Name -eq "that group"' -Properties member | Select-Object -ExpandProperty member | ForEach-Object { Get-ADUser -Identity $_ }

I tested the command for each variable, and they all work on their own. It must be the logic comparing them that is bad, or the output is too different to compare.

I tried comparing them with a foreach loop.

foreach ($user in $OU) {
    if (($include -contains $user.DistinguishedName) -and (-not $exclude -contains $user.DistinguishedName)) {
        Write-Host $user.SAMAccountName
    }
}

..and by piping them through Where-Object.

$filteredUsers = $OU | Where-Object {
    ($include -contains $_.DistinguishedName) -and
    -not ($exclude -contains $_.DistinguishedName)
}

But neither of these statements return output. What am I doing wrong? Is my logic flawed, or am I comparing the wrong values?

I am trying to query a domain for all users from an OU, in a specific group, and not in another specific group.

First, I query Active Directory for all three sets of users: the OU, group to include, and group to exclude. The group to exclude happens to be a distribution group, so I had to query for it a different way.

$OU = Get-ADUser -Filter * -SearchBase "OU=Developers,OU=Everyone,DC=Organization,DC=com" -Properties MemberOf
$include = Get-ADGroupMember -Identity "this group"
$exclude = Get-ADObject -Filter 'Name -eq "that group"' -Properties member | Select-Object -ExpandProperty member | ForEach-Object { Get-ADUser -Identity $_ }

I tested the command for each variable, and they all work on their own. It must be the logic comparing them that is bad, or the output is too different to compare.

I tried comparing them with a foreach loop.

foreach ($user in $OU) {
    if (($include -contains $user.DistinguishedName) -and (-not $exclude -contains $user.DistinguishedName)) {
        Write-Host $user.SAMAccountName
    }
}

..and by piping them through Where-Object.

$filteredUsers = $OU | Where-Object {
    ($include -contains $_.DistinguishedName) -and
    -not ($exclude -contains $_.DistinguishedName)
}

But neither of these statements return output. What am I doing wrong? Is my logic flawed, or am I comparing the wrong values?

Share Improve this question asked Nov 19, 2024 at 1:36 Justin BrunkowJustin Brunkow 638 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

It's much easier and more efficient if you use LDAP to get your users instead of filtering client side:

$groupToInclude = (Get-ADGroup groupToInclude).DistinguishedName
$groupToExclude = (Get-ADGroup groupToExclude).DistinguishedName
$getADUserSplat = @{
    LDAPFilter = "(&(memberOf=$groupToInclude)(!memberOf=$groupToExclude))"
    SearchBase = 'OU=Developers,OU=Everyone,DC=Organization,DC=com'
}
# All users in OU `Developers` that are a member of `$groupToInclude`
# AND NOT a member of `$groupToExclude`
Get-ADUser @getADUserSplat

If you need to get users that are a member of $groupToInclude and not member of $groupToExclude recursively you just need to add the LDAP_MATCHING_RULE_IN_CHAIN operator to your clauses:

"(&(memberOf:1.2.840.113556.1.4.1941:=$groupToInclude)(!memberOf:1.2.840.113556.1.4.1941:=$groupToExclude))"

If you wanted to filter client side, which again, is much more inefficient, you could've done:

$groupToInclude = (Get-ADGroup groupToInclude).DistinguishedName
$groupToExclude = (Get-ADGroup groupToExclude).DistinguishedName
$getADUserSplat = @{
    Filter     = '*'
    SearchBase = 'OU=Developers,OU=Everyone,DC=Organization,DC=com'
    Properties = 'MemberOf'
}
Get-ADUser @getADUserSplat | Where-Object {
    $_.MemberOf -notcontains $groupToExclude -and
    $_.MemberOf -contains $groupToInclude
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745587032a4634603.html

相关推荐

  • powershell - How to Compare Sets of Users - Stack Overflow

    I am trying to query a domain for all users from an OU, in a specific group, and not in another specifi

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信