无法从 Active Directory 中读取所有用户 - [DirectoryServicesCOMException] MoveNext()

Cannot read all users from Active Directory - [DirectoryServicesCOMException] MoveNext()

我的团队正在使用用 C# 编写的程序来读取特定 OU 中的所有用户。该程序的行为很奇怪。有时它会工作几个星期,然后我们的 AD 或任何其他相关组件没有任何大的变化,它会抛出异常。然后它有几个星期没有工作,一段时间后它又开始正常 运行。

代码

    DirectoryEntry searchRoot = new DirectoryEntry("<LDAP string>")

    searchRoot.AuthenticationType = AuthenticationTypes.None;
    DirectorySearcher search = new DirectorySearcher(searchRoot);

    search.Filter = <our filter>;
    search.PropertiesToLoad.Add("<some property>");
    search.PageSize = 1;

    SearchResult result;
    SearchResultCollection resultCol = null;

    try
    {
      resultCol = search.FindAll();
    }
    catch (Exception ex)
    {
      Console.WriteLine(ex.ToString());
    }

    if (resultCol != null)
    {
      Console.WriteLine("Result Count: " + resultCol.Count); //.Count throws the Exception
    }

异常

    Unhandled Exception: System.DirectoryServices.DirectoryServicesCOMException: An operations error occurred.

      at System.DirectoryServices.SearchResultCollection.ResultsEnumerator.MoveNext()
      at System.DirectoryServices.SearchResultCollection.get_InnerList()
      at System.DirectoryServices.SearchResultCollection.get_Count()

Data: System.Collections.ListDictionaryInternal
Error Code: -2147016672
Extended Error: 8431
Extended Error Message: 000020EF: SvcErr: DSID-020A07A7, problem 5012 (DIR_ERROR), data -1018
HResult: -2147016672
Message: An operations error occured.
Source: System.DirectoryServices
Stack Trace: at System.DirectoryServices.SearchResultCollection.ResultsEnumerator.MoveNext()
Target Site: Boolean MoveNext()

附加信息

我试过的

我已经到了一个地步,我对解决这个问题没有更多的想法。感谢您的支持。

这个回答只是在评论中总结了我们的对话。

This thread 部分匹配您得到的错误:

problem 5012 (DIR_ERROR) data -1018

Microsoft MVP 的答案是:

That is a checksum error in the database, you have corruption in your database which is usually due to a failing disk or disk subsystem or possibly a system crash and data not being written from a write cache.

所以听起来您可能正在经历同样的事情。

但可能只有一台DC有问题。因此,为了帮助您缩小选择范围,您可以像这样在 LDAP 路径中指定 DC:

LDAP://dc1.example.com/OU=Target,OU=My User Group,OU=My Users,DC=example,DC=com

这可以在两个方面帮助您:

  1. 它可以识别损坏的 DC,以便您知道需要修复哪一个(并可能在修复之前将其脱机),并且
  2. 您可以专门针对一个好的 DC,以便您的脚本继续工作。