从搜索PowerShell中排除OU

时间:2018-10-04 14:38:02

标签: powershell active-directory

我正在尝试从PowerShell中排除一个OU(子OU)。

这是我的代码:

Get-ADOrganizationalUnit -filter * -SearchBase 'OU=test,DC=test,DC=com' |
 
foreach {
 
$users=Get-ADUser -filter * -searchbase $_.distinguishedname -ResultPageSize 2000 -resultSetSize 500 -searchscope Onelevel | where-object enabled -eq true 
 
$total=($users | measure-object).count
 
New-Object psobject -Property @{
 
OU=$_.Name;
 
A=$Total
 
}
 
}

我是PowerShell的初学者。您能帮我从搜索结果中排除OU吗?

谢谢

1 个答案:

答案 0 :(得分:0)

通过添加if语句:

Get-ADOrganizationalUnit -filter * -SearchBase 'OU=test,DC=test,DC=com' | foreach {

    if($_.distinguishedname -ne "OU=not,OU=that,OU=orgUnit,OU=test,DC=test,DC=com"){

        $users=Get-ADUser -filter * -searchbase $_.distinguishedname -ResultPageSize 2000 -resultSetSize 500 -searchscope Onelevel | where-object enabled -eq true 
        $total=($users | measure-object).count
        New-Object psobject -Property @{
            OU=$_.Name;
            A=$Total
        }
    }
}