使用System.DirectoryServices.ActiveDirectory枚举域控制器会引发异常

时间:2018-02-15 06:50:08

标签: c# active-directory directoryservices

我编写了以下代码来获取林中所有域和DC的映射。但是当我尝试枚举currDomain.DomainControllers某些Domains throws异常时:

  

“无法获取Active Directory域控制器的DNS主机名   使用ntdsa对象名“

但是,如果使用像dsquery这样的其他工具我能够获得所有DC。有人可以解释一下原因吗?

try
{
    foreach (Domain currDomain in Forest.GetCurrentForest().Domains)
    {
        try
        {
            DomainControllerCollection DCs = currDomain.DomainControllers;
            if (DCs.Count > 0)
            {
                //This map is declared already
                dictionaryDomainTree[currDomain.Name] = new List<string>();
                foreach (DomainController controller in DCs)
                {                       
                    dictionaryDomainTree[currDomain.Name].Add(controller.Name);
                }
            }
        }
        catch (System.Exception ex)
        {
            MessageBox.Show("Could not find Domain Controllers in Domain " + currDomain.Name);
            MessageBox.Show(ex.Message);
        }
    }
}
catch (System.Exception ex)
{
    MessageBox.Show("Could not read the Domains in the Forest");
    MessageBox.Show(ex.Message);
    return;
}

0 个答案:

没有答案