【问题标题】:Filter out sub OU in search在搜索中过滤掉子OU
【发布时间】:2012-12-10 17:46:33
【问题描述】:

我正在尝试编写一个 PowerShell 脚本,该脚本将查找 AD 中六个月未登录的所有用户,并且不包括 Terminated Users OU 或 Terminated Users\vendors and others 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。

【问题讨论】:

    标签: powershell search ou


    【解决方案1】:

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

    【讨论】:

    • 谢谢。我什至没有想到。
    【解决方案2】:

    @Mark 提出的解决方案对我不起作用(Windows Server 2016),这个有效

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多