2012-12-10 44 views
4

我正在尝试编写一个PowerShell脚本,该脚本将查找AD中未登录六个月的所有用户,并且不包括终止用户OU或终止用户\供应商和其他OU。我似乎无法得到它排除任何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。

回答

3

更换排除代码和“ft”或“格式表”的顺序。您将数据格式化为没有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

谢谢。这甚至没有发生在我身上。 – OneGuy