我一直面临这个问题,似乎没有结束,希望你能帮助我。
我正在Powershell脚本中针对Active Directory进行LDAP查询。正确到位这是我的代码:
$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://OU=ivegotusers,DC=global,DC=foo,DC=com")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.PageSize = 1000
$objSearcher.Filter = $strFilter
$objSearcher.SearchScope = "Subtree"
#Write-Host $objSearcher
$colProplist = "name", "sn", "whenCreated", "whenChanged", "createTimeStamp", "modifyTimeStamp", "displayName", "mailNickName"
foreach ($i in $colPropList){
$objSearcher.PropertiesToLoad.Add($i)
}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults){
$objItem = $objResult.Properties;
Write-Host -NoNewLine $objItem.name
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.displayName;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.sn;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.whenCreated;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.createTimeStamp;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.whenChanged;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.modifyTimeStamp;
Write-Host "|";
}
所以我们应该为每个查询匹配输出一行,作为行格式如下: 名称|显示名| SN | whenCreated | createTimeStamp | whenChanged | modifyTimeStamp 正确?
我检查过ADSI Edit中的数据,很多用户都有这些数据;然而,输出是
name1||sn1||||
name2||sn2||||
name3||sn3||||
name4||sn4||||
name5||sn5||||
对于那些相同的用户,,并且没有一行包含超过这两个字段的数据。
任何人都可以帮我弄清楚这里发生了什么吗?
注意:由于服务器设置,我无法在Powershell上使用ActiveDirectory导入。
问候并感谢您的时间! 维拉德
答案 0 :(得分:1)
我刚刚被这个人咬了一下:你有两个选择:
属性名称小写:
foreach ($objResult in $colResults){
$objItem = $objResult.Properties;
Write-Host -NoNewLine $objItem.name
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.displayname
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.sn;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.whencreated;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.createtimestamp;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.whenchanged;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.modifytimestamp;
Write-Host "|";
}
或使用.NET集合Item属性样式语法:
foreach ($objResult in $colResults){
$objItem = $objResult.Properties;
Write-Host -NoNewLine $objItem.name
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem["displayName"]
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem.sn;
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem["whenCreated"];
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem["createTimeStamp"];
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem["whenChanged"];
Write-Host -NoNewLine "|";
Write-Host -NoNewLine $objItem["modifyTimeStamp"];
Write-Host "|";
}