One*_*Guy 5 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
Run Code Online (Sandbox Code Playgroud)
我已经从 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10157 次 |
| 最近记录: |