我正在尝试编写一个 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 的步骤。
最佳答案
交换排除代码和“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
关于powershell - 在搜索中过滤掉子 OU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13806303/