繁体   English   中英

当用户没有组时,在 Active Directory 中搜索特定组的成员失败

[英]Searching Active Directory for Members of Specific Group Fails When User Has No Group

提示:本站提供中英文对照查看,鼠标放在中文字句上可显示英文原文。 若本文未解决您的问题,推荐您尝试使用帮您解决。

以下代码应该遍历活动目录中的所有用户并找到特定组中的每个人。 一旦获得用户,它会将他们添加到数据表中,该数据表将成为 gridview 的来源,最终导出到 Excel。

但是,在运行和单步执行时,我注意到它在某个没有组的用户上停止了。 我尝试添加一个条件语句来跳过该实例,但它不起作用,当它找到没有组的用户时,代码仍然停止运行。

有人可以告诉我我可以做些什么不同的事情吗? 警告:这是我继承的一个遗留站点,对活动目录知之甚少,并且是一个新手 vb 编码器。

Dim entry As DirectoryEntry = New DirectoryEntry("LDAP://DOMAIN.EDU", "USERNAME", "PASSWORD", AuthenticationTypes.Secure)
    Dim search As DirectorySearcher = New DirectorySearcher(entry) With {
        .Filter = "(&(objectCategory=User)(objectClass=person))",
        .PageSize = 4000
    }
    search.PropertiesToLoad.Add("userPrincipalName").ToString
    search.PropertiesToLoad.Add("name").ToString
    Dim mySearchResultColl As SearchResultCollection = search.FindAll
    Dim results As DataTable = New DataTable
    results.Columns.Add("User ID")        
    results.Columns.Add("Full Name")
    Dim CurrRow = 0

    For Each sr As SearchResult In mySearchResultColl

        Dim dr As DataRow = results.NewRow
        Dim de As DirectoryEntry = sr.GetDirectoryEntry
        !!!! line below is the problem !!!!
        If de.Properties("memberOf") IsNot Nothing AndAlso de.Properties("memberOf").Value.ToString = "CN=MYGROUP,OU=Security Groups,OU=Students,DC=DOMAIN,DC=edu" Then 
            dr("User ID") = de.Properties("userPrincipalName").Value           
            dr("Full Name") = de.Properties("name").Value
            results.Rows.Add(dr)
            de.Close
        End If
    Next

    gvNot.DataSource = results
    gvNot.DataBind()
    gvNot.Visible = True
    gvAgreed.Visible = False
    ExportToExcel("notagreed", gvNot)

我不确定您所说的“停止”是什么意思,但您可以对其进行更改,使其性能更好并解决您遇到的任何问题。

  1. 这个循环将永远持续下去,因为您必须检查域中的每个用户。 如果您只想要特定组中的用户,那么您可以通过只询问该组的成员来加快速度。 您可以通过在查询中为memberOf属性添加一个条件来做到这一点。 这样,您只会得到您关心的结果。

(请注意,如果您的森林中有多个域,则使用memberOf查找成员可能无法按您希望的方式工作。我在这里写过。但如果您只有一个域,那就没问题了。)

  1. 在循环中,不要为每个结果创建一个DirectoryEntry ,因为这将强制它 go 回到 AD 并再次请求 object 的属性,即使您已经在搜索结果中得到了所需的内容。 因此,请改用SearchResult object 中的值。

  2. SearchResultCollection的文档说:

由于实施限制,SearchResultCollection class 在垃圾回收时无法释放其所有非托管资源。 为防止 memory 泄漏,您必须在不再需要 SearchResultCollection object 时调用 Dispose 方法。

所以你应该把它放在一个Using子句中。

  1. 我也不确定为什么在不使用返回值时在PropertiesToLoad.Add上调用ToString 你可以删除它。

这一切都在一起:

Dim entry As DirectoryEntry = New DirectoryEntry("LDAP://DOMAIN.EDU", "USERNAME", "PASSWORD", AuthenticationTypes.Secure)
Dim search As DirectorySearcher = New DirectorySearcher(entry) With {
    .Filter = "(&(objectCategory=User)(objectClass=person)(memberOf=CN=MYGROUP,OU=Security Groups,OU=Students,DC=DOMAIN,DC=edu))",
    .PageSize = 4000
}
search.PropertiesToLoad.Add("userPrincipalName")
search.PropertiesToLoad.Add("name")

Dim results As DataTable = New DataTable
results.Columns.Add("User ID")        
results.Columns.Add("Full Name")
Dim CurrRow = 0

Using mySearchResultColl As SearchResultCollection = search.FindAll
    For Each sr As SearchResult In mySearchResultColl

        Dim dr As DataRow = results.NewRow
        dr("User ID") = sr.Properties("userPrincipalName")(0)
        dr("Full Name") = sr.Properties("name")(0)
        results.Rows.Add(dr)
    Next
End Using

gvNot.DataSource = results
gvNot.DataBind()
gvNot.Visible = True
gvAgreed.Visible = False
ExportToExcel("notagreed", gvNot)

我没有看到您在任何地方使用CurrRow ,但我留下了它,以防您在此处未显示的其他代码中使用它。

尝试使用 String.Equals function 和 StringComparer.OrdinalIgnoreCase 参数,而不是使用 equals 运算符。 另外你得到的错误是什么?

问题未解决?试试使用:帮您解决问题。
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM