views:

234

answers:

4

I want to get back to get a list of users on a network domain using VB.Net.

I will have the domain name available to me for use.

Thanks in advance.

+1  A: 

Something like this might point you in the right direction, using System.DirectoryServices and System.DirectoryServices.ActiveDirectory:

Private Function GetDomainUsers(ByVal domainDirectoryEntry As DirectoryEntry, ByRef userList As IList) As Integer
Try
    userList = New ArrayList()

    Using domainDirectoryEntry
        Dim ds As New DirectorySearcher(domainDirectoryEntry, "(&(objectCategory=person)(objectClass=user))", New String() {"distinguishedName"})

        Using src As SearchResultCollection = ds.FindAll()
            For Each sr As SearchResult In src
                userList.Add(sr.Properties("distinguishedName")(0))
            Next
        End Using
    End Using

    Return userList.Count
Catch generatedExceptionName As Exception
    userList = Nothing
    Return -1
Finally
    domainDirectoryEntry = Nothing
End Try

End Function

Lee Meyers
I tried that just now and VS does not recognize DirectorEntry as a parameter type.
Sean p
I should add that im trying to do this in .net 2.0
Sean p
Have you added an Imports statement for the System.DirectoryServices namespace in your code, and added a reference to the System.DirectoryServices.dll to your project?
Lee Meyers
It is available in .NET 2.0 by the way
Lee Meyers
Im a pretty novice programmer. The documentation I found on MSDN is pretty shoddy. Any better direction? What i would like best would be to get a list of users on a domain (which i have the name of) and from a group on that domain (which i have as well)
Sean p
A: 

Another option would be exploring System.Management and System.Management.Instrumentation.Here is a short snippet of how you pull the users of a particular domain using these namespaces.

Imports System.Management
Imports System.Management.Instrumentation  

Sub PrintDomainUsers()  

        Dim domainName As String = System.Environment.UserDomainName.ToString
        Dim userQuery As SelectQuery = New SelectQuery("Win32_UserAccount", "Domain='" & domainName & "'")
        Try
            Dim userSearch As ManagementObjectSearcher = New ManagementObjectSearcher(userQuery)
            For Each domainUser In userSearch.Get
                Console.WriteLine(domainUser("Name"))
            Next

        Catch ex As Exception
            Throw ex
        End Try

End Sub
I Am Back
Use `System.DirectoryServices` over WMI for Active Directory any day. The strong typing is only one major gain for using the managed classes.
cottsak
A: 

Imports System.Management Imports System.Management.Instrumentation

Sub PrintDomainUsers()

    Dim domainName As String = System.Environment.UserDomainName.ToString
    Dim userQuery As SelectQuery = New SelectQuery("Win32_UserAccount", "Domain='" & domainName & "'")
    Try
        Dim userSearch As ManagementObjectSearcher = New ManagementObjectSearcher(userQuery)
        For Each domainUser In userSearch.Get
            Console.WriteLine(domainUser("Name"))
        Next

    Catch ex As Exception
        Throw ex
    End Try

End Sub

This works but how do i filter by a certain group. Im getting THOUSANDS of resutls

Sean p
I know that I need to add to the condition statement but I do not know what to add. HELP!?!?
Sean p
A: 

Anyone have anymore insight?

Sean p