powershell - 在搜索中过滤掉子 OU

标签 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 的步骤。

最佳答案

交换排除代码和“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/

相关文章:

Java - JCheckBox 针对列表进行过滤

c# - 检查用户是否存在于ou中

powershell - 如何在 PowerShell 中重命名别名?

Powershell : How to install PSReadline module in an offline computer

powershell - Windows 期望命令等效

java - java中如何进行搜索?

c++ - 在 unordered_map 中搜索的最佳实践

powershell - 如何使用 CMD 从服务器本身查找计算机对象 OU

powershell - 要求用户从存储的列表中选择选项(AD OU 路径)

powershell - Powershell脚本重新索引WSUS DB和电子邮件结果