统计Active Directory用户

时间:2017-09-21 06:50:01

标签: powershell active-directory powershell-v2.0

下面的代码应该计算并比较用户并输出总计数,但不知何故结果为空。我需要做些什么来解决它?

$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

2 个答案:

答案 0 :(得分:0)

我想你想要算上两组的独特成员:

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

答案 1 :(得分:0)

你可以用更加强大的方式来做到这一点。

$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"