在搜索中过滤掉子OU

时间:2012-12-10 17:46:33

标签: powershell search ou

我正在尝试编写一个PowerShell脚本,该脚本将查找AD中尚未登录六个月的所有用户,并且不包括Terminated Users OU或Terminated Users \ vendors和其他OU中的任何人。我似乎无法排除OU。搜索的六个月部分完美无缺。

这是我目前的代码:

Search-ADAccount -accountinactive -datetime (get-date).AddMonths(-6) -usersonly | ft Name,LastLogonDate | ? {$_.DistinguishedName -notlike "*ou=Terminated Users,*" -and $_.DistinguishedName -notlike "*ou=vendors and others,*"} | Out-File stale_users.txt

我已经从OU名称的末尾删除了*,尝试了-or,并且自己尝试了每个OU。它仍然不会跳过搜索那些OU的。

1 个答案:

答案 0 :(得分:3)

交换排除代码和“ft”或“Format-Table”的顺序。您将数据格式化为没有DistinguishedName字段的位置,然后尝试匹配该缺少的字段。

Search-ADAccount -accountinactive -datetime (get-date).AddMonths(-6) -usersonly | `
  ? {$_.DistinguishedName -notlike "*ou=Terminated Users,*" -and $_.DistinguishedName -notlike "*ou=vendors and others,*"} |`
  ft Name,LastLogonDate |`
  Out-File stale_users.txt
相关问题