2016-08-16 64 views
0

一个微不足道的问题,但希望对那些知道的人来说真的很明显。ADSI/System.DirectoryServices.DirectorySearcher结果解析

搜索构造函数:

$Search = New-Object System.DirectoryServices.DirectorySearcher 
(([adsi]"LDAP://ou=Domain Users,dc=example,dc=pri"),'(objectCategory=person)', 
('name','employeeID')) 

我要排除的结果,其中雇员的属性不存在。

这工作:

$users = $Search.FindAll() 
ForEach ($u in $users) { 
    If ($u.properties.employeeid) { 
     Write-Host $($u.properties.name) 
    } 
} 

下不工作 - 无输出。但是,当省略IF语句时,会输出结果。

ForEach ($user in $($Search.FindAll())) { 
    If ($user.properties.employeeID) { 
     Write-Host $($user.properties.name) 
    } 
} 

这是第二个例子中的语法问题,还是我只是需要临时存储结果在对象上运行条件语句之前? (为了避免讨论关于为什么不使用ActiveDirectory模块和Get-ADUser的问题,它适用于不能在他们的工作站上安装模块的用户,也不能被授权通过它所在的主机上的PSSession调用它安装)

更新:发现做where条款的轻微更好的方式:

$searcher.FindAll() | where { ($_.properties['employeeid'][0]) } 

回答

0

只是删除if声明和过滤搜索结果:

$users = $Search.FindAll() | Where-Object {-not [string]::IsNullOrEmpty($_.properties.employeeID)} 
+0

感谢您的解决方法,并且将它作为过滤器更好。 – Trix