Solved

Query Active Directory From VB.NET

Posted on 2014-03-12
4
1,895 Views
Last Modified: 2016-02-02
I need to pull a list of usernames from active directory that have a particular value in a custom attribute.

I have the AD query which was built in the Active Directory Users and Computers tool and works just fine:

(&(objectCategory=user)(objectClass=user)(extensionAttribute1=myCustomValue))

how can I run this in VB.NET and add each resulting user to a drop-down list?

I hope that makes sense :)
0
Comment
Question by:BrianFord
[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
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39925894
Firstly you'll need to add a reference to the System.DirectoryServices assembly, and an Import statement like:
Imports System.DirectoryServices

Open in new window

Then you can use something like (where ddl is the name of the dropdownlist):
        Dim search As New DirectorySearcher
        search.Filter = "(&(objectCategory=user)(objectClass=user)(extensionAttribute1=myCustomValue))"
        search.PropertiesToLoad.AddRange(
            New String() {"SAMAccountName", "givenname", "sn"}
            )

        Dim results As SearchResultCollection = search.FindAll()
        For Each result As SearchResult In results
            ddl.Items.Add(result.Properties("givenname")(0))
        Next

Open in new window

You may need to tweak it for security, etc, depeding on how your AD is configured.
0
 

Author Comment

by:BrianFord
ID: 39925961
thank you, I will try this out shortly
0
 

Author Closing Comment

by:BrianFord
ID: 39926036
Works like a charm

thanks again
0
 

Expert Comment

by:hamzajosh
ID: 41445349
I tried the above code and get a message saying "The specified domain either does not exist or could not be contacted." on the search.findall() code
Where do i specify the domain to search for in the code. I am trying it on my standalone laptop for now without connecting to any domain FYI.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

635 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