获取网络域上的用户列表

发布于 2024-08-12 06:34:29 字数 69 浏览 2 评论 0原文

我想返回使用 VB.Net 获取网络域上的用户列表。

我将拥有可供我使用的域名。

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

漆黑的白昼 2024-08-19 06:34:29

它可能会在选择查询中引发错误。

请检查:

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

在 VS 中,单击“项目”菜单 >“项目”>“项目”。添加参考。
在 .Net 选项卡上,向下滚动直至看到 System.Management。单击它进行选择,然后单击“确定”。
现在回到您的代码,在类的最顶部,输入“Imports System.Management”,您应该已准备就绪。

来源:

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

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

It might throw an error in the select query.

Please check this:

Did you add a reference to the System.Management assembly to your project? If you haven't, do this:

In VS, click on Project menu > add reference.
On the .Net tab, scroll down until you see System.Management. Click on it to select, then click OK.
Now back in your code, at the very top of your class, put in "Imports System.Management", and you should be all set.

Source:

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

It worked for me without any issues. I am able to get all user names for the domain.

信愁 2024-08-19 06:34:29

可能会为您指明正确的方向

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

使用 System.DirectoryServices 和 System.DirectoryServices.ActiveDirectory: End Function

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

初熏 2024-08-19 06:34:29
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

这可行,但我如何按特定组进行过滤。我收到了数千个结果

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

新雨望断虹 2024-08-19 06:34:29

另一种选择是探索 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文