【发布时间】:2017-11-01 18:37:45
【问题描述】:
我正在寻找检索所有 Active Directory 帐户名字和姓氏的最快和最简单的方法。目的是有一个字符串列表,其中包含来自 Active Directory 的所有名称,以便在用于解锁帐户的小型 Visual Basic 应用程序的文本框中自动完成。
使用场景:
在表单加载时,应用程序会从 AD 生成名称列表。我预计这需要大约 10-15 秒,因为 AD 上有 4,500 个帐户。每个名称都被添加到一个字符串列表中,用于自动完成。
用户在textbox1 中键入名称 Garry,自动完成功能会使用字符串列表建议 AD 中的所有 Garry。我知道如何轻松做到这一点,我只是不知道如何有效地用用户名填充列表。
有很多关于访问 AD 的示例,但没有一个显示如何循环访问它。我认为在这里询问会在类似的用例中帮助我自己和其他用户。
到目前为止,我用于访问单个帐户的代码如下所示,但是我需要遍历 所有 AD 帐户并检索他们的名字和姓氏。
访问单个帐户的当前代码:
'Domain is declared with the LDAP path
'UserName is declared with textbox1.text value
Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://" & Domain)
Dim ADSearch As New System.DirectoryServices.DirectorySearcher(ADEntry)
ADSearch.Filter = ("(samAccountName=" & UserName & ")")
ADSearch.SearchScope = System.DirectoryServices.SearchScope.Subtree
Dim UserFound As System.DirectoryServices.SearchResult = ADSearch.FindOne()
If Not IsNothing(UserFound) Then
Log.AppendLine("Account found, loading checks...")
Dim Attrib As String = "msDS-User-Account-Control-Computed"
Dim User As System.DirectoryServices.DirectoryEntry
User = UserFound.GetDirectoryEntry()
User.RefreshCache(New String() {Attrib})
'Display user account details
txtLogin.Text = User.Properties("userPrincipalName").ToString
txtName.Text = User.Properties("givenName").ToString & " " & User.Properties("sn").ToString
else
'User not found
end if
任何帮助都将不胜感激,即使在 C# 中也是如此。
【问题讨论】:
-
如果您在工作站上安装了 RSAT 工具,您可以使用 PowerShell 轻松完成此操作。这是您的选择吗?
-
不,我在锁定的远程机器上工作,所以无法安装任何第三方工具 :(
-
我在 VB 或 LDAP 上都不强,但我认为您在这里走在正确的轨道上。您的 LDAP 过滤器可以包含通配符(请参阅MSDN on LDAP Filter Syntax),并且通过使用
FindAll()方法而不是FindOne(),您可以返回与您的过滤器匹配的对象集合(请参阅MSDN on the DirectorySearcher class)。