【发布时间】:2017-05-31 14:31:27
【问题描述】:
目标:创建一个简单的 VB.NET 应用程序以使用基本过滤器扫描 GlobalCatalog,仅限于预定义的属性并将结果写入文本文件。
方法:下面的现有代码 - 这“有效”但偶尔会引发异常:“System.DirectoryServices.SearchResultCollection.ResultsEnumerator.MoveNext():更多数据可用”
一些浏览使我认为(有待更正)该问题是由于尝试通过 DirectorySearcher 检索大量记录(在我的情况下大约为 400k)引起的,尽管结果是分页的,并且解决方案可能是使用 System.DirectoryServices.Protocols 切换现有的 System.DirectoryServices 方法。请参阅this SO thread 导致this article。
但是,我发现的所有响应,包括上面的链接和其他来自广泛搜索的链接,都只提供 C# 中的代码 sn-ps,并且似乎只查询单个记录(例如,根据特定的 distinctName 检索属性或登录)
我需要使用 VB.NET 尽可能快速高效地检索大量记录。我喜欢 DirectoryServices 方法,因为它让我可以轻松处理 GlobalCatalog,而无需提供域或密码 - 我可以直接跳转到搜索器并开始指定过滤器和属性。它通常有效 - 但我每次都需要它。
谁能建议我如何调整此代码以规避偶尔出现的异常并以最佳方式撤回我需要的所有数据?
Imports System.DirectoryServices
Public Sub ScanGlobalCatalog()
Dim searcher As DirectorySearcher = ActiveDirectory.Forest.GetCurrentForest.FindGlobalCatalog.GetDirectorySearcher
Try
With searcher
.Filter = "(&(|(objectClass=user)(objectClass=group))(proxyAddresses=*))"
.PageSize = 1000
.SearchScope = SearchScope.Subtree
.CacheResults = False
.PropertiesToLoad.Add("sAMAccountName")
.PropertiesToLoad.Add("distinguishedName")
.PropertiesToLoad.Add("displayName")
.PropertiesToLoad.Add("proxyAddresses")
End With
For Each result As SearchResult In searcher.FindAll()
Dim properties As ResultPropertyCollection = result.Properties
Dim sAMAccountName As ResultPropertyValueCollection = properties("sAMAccountName")
Dim distinguishedName As ResultPropertyValueCollection = properties("distinguishedName")
Dim displayName As ResultPropertyValueCollection = properties("displayName")
Dim proxyAddresses As ResultPropertyValueCollection = properties("proxyAddresses")
' Check / process / write each property to the output file...
Next
Catch ex As Exception
' Do something...
End Try
End Sub
【问题讨论】:
-
检查
searcher是否有resultsize属性,并尝试发送 maxint 或任何实际等效的“无限”。
标签: vb.net active-directory directoryservices