Results 1 to 12 of 12

Thread: Get all members of an Active Directory group or local group

  1. #1
    Pro Grammar chris128's Avatar
    Join Date
    Jun 07
    Location
    England
    Posts
    7,534

    Get all members of an Active Directory group or local group

    Sorry if anyone else has already posted something similar but I did search and didnt see anything

    I helped someone out with this in a thread today/yesterday and thought I'd post it here for others to use in the future if they want to do a similar thing.

    You can use this to find a list of all of the members of any group in AD. You will need to add a reference to System.DirectoryServices first though.
    vb.net Code:
    1. Dim GroupSearcher As New DirectorySearcher
    2. 'Change the OU path, domain and domain admin details
    3. Dim GroupSearchRoot As New DirectoryEntry("LDAP://OU=YourGroupsOU,DC=yourdomainname,DC=com", "Your_Domain_Admin", "Admin_Password")
    4.  
    5. With GroupSearcher  
    6.        .SearchRoot = GroupSearchRoot    
    7.        .Filter = "(&(ObjectClass=Group)(CN=YourGroupName))"  '<<< Change the Group name here
    8. End With
    9.  
    10. Dim Members As Object = GroupSearcher.FindOne.GetDirectoryEntry.Invoke("Members", Nothing) '<<< Get members
    11. For Each Member As Object In CType(Members, IEnumerable)  '<<< loop through members
    12.   Dim CurrentMember As New DirectoryEntry(Member) '<<< Get directoryentry for user
    13.   ListBox1.Items.Add(CurrentMember.Name.Remove(0, 3))  '<<< Add user's CN(common name) to listbox
    14. Next

    As you can see in my example, I am using a listbox named Listbox1 to display all of the user's names. If you dont want to do that then just remove that line and use the directoryentry object for each user to get whatever attributes you want etc

    Hope it helps someone out

    Chris
    Last edited by chris128; Jan 26th, 2009 at 06:02 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 07
    Location
    England
    Posts
    7,534

    Re: Get all members of an Active Directory group

    Just to add to this, you can use a similar technique for getting group members from a local computer. Just use the WinNT provider instead of the LDAP provider.

    Here is an example that gets the users of the local Administrators group from a specified computer:

    vb.net Code:
    1. Dim MachineName As String = "Some_PC_Name" '<<< Put the PC name or IP here that you want to connect to
    2. Dim Admins As New DirectoryEntry("WinNT://" & MachineName & "/Administrators") 'Connect to machine
    3.                
    4. Dim Members As Object = Admins.Invoke("Members", Nothing) 'Get members
    5. For Each Member As Object In CType(Members, IEnumerable)  'loop through members
    6.           Dim CurrentMember As New DirectoryEntry(Member) 'Get directoryentry for user
    7.           MessageBox.Show(CurrentMember.Name) 'Show the user's name in a messagebox
    8. Next
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    New Member
    Join Date
    Jun 10
    Posts
    4

    Re: Get all members of an Active Directory group or local group

    Excellent post. What if I wanted to enumerate all users on a local computer, and not just ones from a specific group?

  4. #4
    New Member
    Join Date
    Jun 10
    Posts
    4

    Re: Get all members of an Active Directory group or local group

    Quote Originally Posted by BertMan View Post
    Excellent post. What if I wanted to enumerate all users on a local computer, and not just ones from a specific group?
    Sorry, meant to specify using the WinNT provider....

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 07
    Location
    England
    Posts
    7,534

    Re: Get all members of an Active Directory group or local group

    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6
    New Member
    Join Date
    Jun 10
    Posts
    4

    Re: Get all members of an Active Directory group or local group

    Thanks!!

    Can you explain to me or point me to some documentation that explains where the WinNT provider is pulling this info? Also, I am going to need to do a lot of queries to local accounts, and if there is something out there that can list all the different properties and methods that would be great. For example, the next thing I need to do is find out if a local account is disabled, and disable it if need be. Where do I find out what properties are availible to the user object?

  7. #7
    New Member
    Join Date
    Jun 10
    Posts
    4

    Re: Get all members of an Active Directory group or local group

    Sorry, just realized that I am in the code bank section. Mods, feel free to delete my posts and I will post in the correct location.

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 07
    Location
    England
    Posts
    7,534

    Re: Get all members of an Active Directory group or local group

    Quote Originally Posted by BertMan View Post
    Thanks!!

    Can you explain to me or point me to some documentation that explains where the WinNT provider is pulling this info? Also, I am going to need to do a lot of queries to local accounts, and if there is something out there that can list all the different properties and methods that would be great. For example, the next thing I need to do is find out if a local account is disabled, and disable it if need be. Where do I find out what properties are availible to the user object?
    I've no idea how exactly the WinNT provider gets the information I'm afraid but here is a reference of all the properties that you should be able to get from a user account you retrieved with it: http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx and there is also this article that mentions some properties which are not available when using the WinNT provider: http://msdn.microsoft.com/en-us/libr...(v=VS.85).aspx
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9
    New Member
    Join Date
    Sep 10
    Posts
    2

    Re: Get all members of an Active Directory group or local group

    Hi Chris,
    very great code for the group members in the active directory, but I habe a little problem:

    I must use the result of the Listbox to query another database.
    If I select a item in the List box i canīt use it for the other query.
    I tested to show me the selected item in a textbox.text, but it doesnīt work.

    I donīt find the bug in the code.

    Can you help me, please?

    best regards
    Daniel

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 07
    Location
    England
    Posts
    7,534

    Re: Get all members of an Active Directory group or local group

    Quote Originally Posted by DanLen View Post
    Hi Chris,
    very great code for the group members in the active directory, but I habe a little problem:

    I must use the result of the Listbox to query another database.
    If I select a item in the List box i canīt use it for the other query.
    I tested to show me the selected item in a textbox.text, but it doesnīt work.

    I donīt find the bug in the code.

    Can you help me, please?

    best regards
    Daniel
    That sounds like nothing to do with this code, just general coding advice, so post it in the VB.NET forum
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11
    New Member
    Join Date
    Sep 10
    Posts
    1

    Re: Get all members of an Active Directory group or local group

    Hi Chris

    Great post!

    I have 2 questions.

    1. Is it possible to list the domains of the users?....Domain\useraccount
    MachineName\Administrator.
    2. Is it possible to connect to the WinNT provider with administrative credentials?

    Thanks

  12. #12
    New Member
    Join Date
    Sep 10
    Posts
    0

    Re: Get all members of an Active Directory group

    Quote Originally Posted by chris128 View Post
    Just to add to this, you can use a similar technique for getting group members from a local computer. Just use the WinNT provider instead of the LDAP provider.

    Here is an example that gets the users of the local Administrators group from a specified computer:

    vb.net Code:
    1. Dim MachineName As String = "Some_PC_Name" '<<< Put the PC name or IP here that you want to connect to
    2. Dim Admins As New DirectoryEntry("WinNT://" & MachineName & "/Administrators") 'Connect to machine
    3.                
    4. Dim Members As Object = Admins.Invoke("Members", Nothing) 'Get members
    5. For Each Member As Object In CType(Members, IEnumerable)  'loop through members
    6.           Dim CurrentMember As New DirectoryEntry(Member) 'Get directoryentry for user
    7.           MessageBox.Show(CurrentMember.Name) 'Show the user's name in a messagebox
    8. Next
    Hi Chris excellent post.

    How can you authenticate with an admin account when using the winNT provider to get the local admin group members?

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •