获取网络域上的用户列表

时间:2009-11-25 18:38:30

标签: vb.net dns

我想回过头来使用VB.Net获取网络域中的用户列表。

我可以使用域名供我使用。

提前致谢。

4 个答案:

答案 0 :(得分:3)

它可能会在选择查询中抛出错误。

请检查:

您是否已将System.Management程序集的引用添加到项目中?如果还没有,请执行以下操作:

在VS中,单击“项目”菜单>添加参考 在.Net选项卡上,向下滚动,直到看到System.Management。单击它以选择,然后单击“确定” 现在回到你的代码中,在你的课程的最顶层,输入“Imports System.Management”,你应该全部设置好。

来源:

http://www.vbforums.com/showthread.php?t=560422

它没有任何问题对我有用。我能够获得该域的所有用户名。

答案 1 :(得分:1)

使用System.DirectoryServices和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

结束功能

答案 2 :(得分:1)

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

这有效,但我如何按特定组过滤。我得到了数以千计的结果

答案 3 :(得分:0)

另一种选择是探索System.ManagementSystem.Management.Instrumentation。以下是使用这些命名空间拉取特定域用户的简短摘要。

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