过滤掉搜索中的子OU

问题描述 投票:5回答:2

我正在尝试编写一个PowerShell脚本,该脚本将查找AD中所有六个月未登录的用户,并且不将任何用户包括在终止用户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名称的末尾删除了,*,尝试了-或,并且仅尝试了每个OU。它仍然不会跳过搜索这些OU。

powershell search ou
2个回答
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

0
投票

@ 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
© www.soinside.com 2019 - 2024. All rights reserved.