Solved

Query Active Directory From VB.NET

Posted on 2014-03-12
4
1,789 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

710 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