1

I've been facing this problem and it doeen't seems to have an end, hope you can help me.

I'm making an LDAP query against Active Directory within a Powershell script. Getting right to the point this is my code:

$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 "|";
}

So we should get as output a line for each query match, being the line format as follows: name|displayName|sn|whenCreated|createTimeStamp|whenChanged|modifyTimeStamp right?

I've checked the data in the ADSI Edit and a lot of users had that data; nevertheless, the output was

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

for those same users, and there's not a single line that has more than these two fields seted.

Can anyone help me figuring out what's happening in here?

Note: Due to server set-ups, I'm not able to use the ActiveDirectory import on Powershell.

Regards and thanks for your time! Vlad

1 Answer 1

1

I got bitten by this one a while ago: you've got two choices:

propertyNames in lower case:

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 "|";
}

or use the .NET collection Item property style syntax:

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 "|";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe it worked! Just the all-lower-case properties names did the trick. And of course, I'm now wondering how I managed not to try this before myself. Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.