1

我一直面临这个问题,它似乎没有结束,希望你能帮助我。

我正在 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 "|";
}

所以我们应该为每个查询匹配输出一行,行格式如下:name|displayName|sn|whenCreated|createTimeStamp|whenChanged|modifyTimeStamp 对吗?

我检查了 ADSI Edit 中的数据,很多用户都有这些数据;然而,输出是

name1||sn1||||
name2||sn2||||
name3||sn3||||
name4||sn4||||
name5||sn5||||

对于那些相同的用户,没有一行设置超过这两个字段。

谁能帮我弄清楚这里发生了什么?

注意:由于服务器设置,我无法在 Powershell 上使用 ActiveDirectory 导入。

问候并感谢您的宝贵时间!弗拉德

4

1 回答 1

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 集合项属性样式语法:

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 "|";
}
于 2013-05-05T07:44:18.867 回答