Solved

vb.net ldap query departmentnumber not working

Posted on 2014-10-29
5
159 Views
Last Modified: 2014-10-30
I am attempting to write a list of users that are assigned to a specific departmentnumber in LDAP, which i know should be a list of about 100.  The below code only pulls back one member (which last name starts with T so in my mind it seems like its only returning the last value):

Dim userIds As IEnumerable(Of String) = {"7871"}
        For Each i As String In userIds
            Dim de As New DirectoryEntry("LDAP://test.net:389/DC=test,DC=net")
            Dim LdapFilter As String = "(departmentNumber=" & i & ")"
            Dim searcher As New DirectorySearcher(de, LdapFilter)
            Dim result As SearchResult = searcher.FindOne()
            Dim res As SearchResultCollection = searcher.FindAll()
            Dim item As ListViewItem = ListView1.Items.Add(i)
            item.SubItems.Add(result.Properties("givenName")(0).ToString())
            item.SubItems.Add(result.Properties("cn")(0).ToString())
            item.SubItems.Add(result.Properties("userPrincipalName")(0).ToString())
                next

Open in new window

0
Comment
Question by:derek7467
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40411203
You are using FindOne method which returns you a single user. You need to looping through your res variable as that's a collection of search results.
0
 

Author Comment

by:derek7467
ID: 40411374
How would i do that?
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 40412089
Try

Dim userIds As IEnumerable(Of String) = {"7871"}
        For Each i As String In userIds
            Dim de As New DirectoryEntry("LDAP://test.net:389/DC=test,DC=net")
            Dim LdapFilter As String = "(departmentNumber=" & i & ")"
            Dim searcher As New DirectorySearcher(de, LdapFilter)
            Dim result As SearchResult
            Dim res As SearchResultCollection = searcher.FindAll()
          For Each Result In Res
            Dim item As ListViewItem = ListView1.Items.Add(i)
            item.SubItems.Add(result.Properties("givenName")(0).ToString())
            item.SubItems.Add(result.Properties("cn")(0).ToString())
            item.SubItems.Add(result.Properties("userPrincipalName")(0).ToString())
          Next
      next

Open in new window

0
 

Author Closing Comment

by:derek7467
ID: 40413020
worked perfect, thanks!

I dont like to just take the work and not understand so it looks like you cycle through the department code pull everyone back and then for each entry pulled back you cycle through again and pull properties for each, do i have that right?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40414723
Yes that's correct.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question