如何获取其 OU 包含关键字的用户:终止

how to get users whose OU includes the keyword: terminated

在 Active Directory 中,我有“Ex Domain Users”文件夹,其中包括一堆文件夹。有四个具有“已终止”关键字的文件夹,我需要从中检索用户。

在我的 powershell 脚本中,我这样做如下:

$users  = Get-ADUser -Filter * -SearchBase “OU=Terminated,OU=Ex Domain Users,DC=xxx,DC=local”; 
$users += Get-ADUser -Filter * -SearchBase “OU=Terminated (ESA),OU=Ex Domain Users,DC=xxx,DC=local”;
$users += Get-ADUser -Filter * -SearchBase “OU=Terminated (Last week),OU=Ex Domain Users,DC=xxx,DC=local”; 
$users += Get-ADUser -Filter * -SearchBase “OU=Terminated (Last month),OU=Ex Domain Users,DC=xxx,DC=local”;

我正在寻找表达 ... -Searchbase "OU in ('%Terminated%'), ...") 的方式,但找不到正确的语法或方法。任何帮助将不胜感激。

此致。

在针对每个调用 Get-ADUser 之前使用 Get-ADOrganizationalUnit 枚举相关 OU:

$targetOUs = Get-ADOrganizationalUnit -Filter 'Name -like "*Terminated*"'

$users = $targetOUs |ForEach-Object {
  Get-ADUser -Filter * -SearchBase $_.distinguishedName
}

请注意,Get-AD* cmdlet 默认执行 subtree 查询,但如有必要,您可以将范围限制为 OU 的直接子级:

Get-ADUser -SearchScope OneLevel ...