LDAP queries and values

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 12 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

LDAP queries and values

Post by boyddt_co »

I'm hoping that this is the correct place to post and if not can you
direct me where I should post. Not necessarily a PrimalScript issue.I
am trying to query LDAP (ActiveDirectory) to pull user information from
a particular OU. I get the data and it presents on the screen
successfully. But what I'm finding out is that the data returned is
actually an object and I can't pass it to another process. What I need
to get is the value of the distinguished name.Any help would be greatly appreciated.Function TestUser { $filter = "(&(objectClass=user)(objectCategory=person))" $ds = [adsiSearcher]$filter $ds.searchRoot = $userOU $users = $ds.findall() $usersCount = $users.count Write-Host $usersCount $i = 0 foreach($suser in $users) { $userCN = $suser.properties.cn $userDN = $suser.properties.distinguishedname $userCNOjbect = $userCN -is [object] $userDNOjbect = $userDN -is [object] Write-Host "CN is object "$userCNOjbect Write-Host "DN is object "$userDNOjbect Write-Host "User DistinguishedName "$userDN Write-Host "User CN "$userCN Write-Host "" $i++ Write-Host ($i/$usersCount) * 100 } }$userOU = "LDAP://OU=Jennersville,DC=synthes,DC=com""LDAP:TestUser
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

LDAP queries and values

Post by jvierra »

Try this:

$filter = "(&(objectClass=user)(objectCategory=person))" $ds = [adsiSearcher]$filter $ds.searchRoot = $userOU $ds.findall() |
ForEach-Object{
$_.path
}
User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

LDAP queries and values

Post by boyddt_co »

I took your code and put it into my script, when I run it I get the following message:cmdlet ForEach-Object at command pipeline position 1Supply values for the following parameters:Process[0]:This is the code I haveFunction ScriptingAnswers { $filter = "(&(objectClass=user)(objectCategory=person))" $ds = [adsiSearcher]$filter $ds.searchRoot = $userOU $ds.findall() | ForEach-Object { $_.path } }$userOU = "LDAP://OU=Jennersville,DC=synthes,DC=com"ScriptingAnswersAny thoughts?David
User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

LDAP queries and values

Post by boyddt_co »

Hey, thanx for the explanation. I did move the bracket next to the foreach-object and that fixed it. As for the books, what do you suggest? I've downloaded Windows Powershell Pocket Reference but I'm looking for something more robust for a learning tool.David
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

LDAP queries and values

Post by jvierra »

I think this is an excellent starting place:
http://www.sapien.com/books

It is from the <books> link at the top of this page. It is quick, easy to understand, and has hundreds of practival exapmples and demos of POwerShell syntax and mechanics. It is one of the best ot the learn PowerShell books. I have both TFM 1 and TFM 2.

There are many other PowerShell books but you need to start at the bottom and master the basics before the advanced books become fully useful.

User avatar
boyddt_co
Posts: 89
Last visit: Mon Sep 25, 2023 9:36 pm

LDAP queries and values

Post by boyddt_co »

Sometimes I feel so thick. Using the information above $_.properties['cn'] I get the information as requested but it is an object where what I would really like is just the text string. I've tried

$_.properties['cn'].tostring() and $_.properties['cn'].value and though they are no longer objects they also don't have any information. What am I doing wrong?As a side note, I did buy that book and hopefully I will be asking fewer questions as time goes on.Thanx in advance.
This topic is 12 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked