简体   繁体   中英

Count Active Directory users

The code below is supposed to count and compare user and output the total count, but somehow the result is empty. What do I need to do to fix it?

$groups = $A_group, $B_Group
$gm     = @()

foreach ($group in $groups) {
    $gm += Get-ADGroupMember $group -Recursive |
           `where {$_.objectclass -eq 'user'} |`  
           ' select SamAccountName'
}

($gm.samaccountname | Select -Unique).Count
Write-Output total: ($gm.samaccountname | Select -Unique).Count

I think you want to count of the unique members of two groups:

$aGroup = @('a','b','c')
$bGroup = @('b','c','e','f')
(Compare-Object $aGroup $bGroup -IncludeEqual).count

You can do this in a much more powershell-esque way.

$groups  = $A_group, $B_Group
$uniqueMemberCount = $groups |
    Get-ADGroupMember -Recursive |
    Where-Object {$_.objectClass -ieq "user"} |
    Select-Object -Unique |
    Measure-Object |
    Select-Object -ExpandProperty Count

Write-Output "Total: $uniqueMemberCount"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM