结合每个陈述

时间:2014-09-02 11:23:20

标签: powershell for-loop active-directory

我正在尝试获取每个活动目录OU中的计算机数量,这是我能够做到的。我也试图获取每个OU中属于Test_Group(P)成员资格组的计算机数量,我也可以这样做。我只是不知道如何在同一行输出?所以我想选择名称,计算机,那么$ Test的值也是如此?下面是我想要实现的一个例子,现在我只能得到名称和计算机输出?

名称计算机测试

US 100 3

CA 20 1

import-module activedirectory
 foreach ($ou in  Get-ADOrganizationalUnit  -filter * -SearchScope 1){
   $computers = 0 + (get-adcomputer -filter * -searchbase $ou.distinguishedname).count 
     $Test =  (Get-ADGroupMember -Identity “Test_Group (P)”).count 
     $ou | add-member -membertype noteproperty -name Computers -value $computers -force
     $ou | select name,computers

    }

1 个答案:

答案 0 :(得分:1)

好的,试试这个:

Import-Module ActiveDirectory
foreach($ou in Get-ADOrganizationalUnit -Filter * -SearchScope 1){
    $computers = Get-ADComputer -Filter * -SearchBase $ou.DistinguishedName -Properties 'memberof'
    [int]$numComputers = $computers.count
    [int]$numMemberOfTestGroup = ($computers | Where-Object {$_.memberof -match 'TEST_Group (P)'}).count
    Write-Output ([PSCustomObject] @{
        OU = $ou.Name
        NumComputers = $numComputers
        NumTestMembers = $numMemberOfTestGroup
    })
}