Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query Active Directory From VB.NET

Posted on 2014-03-12
4
Medium Priority
?
2,131 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
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
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.
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…

772 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