Unable to get all DirectoryEntry in a Forest

0

I'm trying to get all directory entries in a forest.

My code snippet is shown below:

DirectoryContext dc = new DirectoryContext(DirectoryContextType.DirectoryServer, "xx.x.xxx.40", "w28\\administrator", "pwd");

Forest forest = Forest.GetForest(dc);

Console.WriteLine("Domain count in forest: " + forest.Domains.Count);
DomainCollection domains = forest.Domains;

foreach (Domain d in domains)
{
    Console.WriteLine(d.Name);

    //It doesn't work
    DirectoryEntry entry = d.GetDirectoryEntry();
    foreach (DirectoryEntry child in entry.Children)
    {
        Console.WriteLine(" - " + child.Name);
    }
}

However, I get an exception:

System.DirectoryServices.ActiveDirectory.ActiveDirectoryOperationException: Unknown error (0x80005000) ---> System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.PropertyValueCollection.PopulateList()
   at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
   at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
   at System.DirectoryServices.ActiveDirectory.PropertyManager.GetPropertyValue(DirectoryContext context, DirectoryEntry directoryEntry, String propertyName)
   --- End of inner exception stack trace ---
   at System.DirectoryServices.ActiveDirectory.PropertyManager.GetPropertyValue(DirectoryContext context, DirectoryEntry directoryEntry, String propertyName)
   at System.DirectoryServices.ActiveDirectory.DirectoryEntryManager.ExpandWellKnownDN(WellKnownDN dn)
   at System.DirectoryServices.ActiveDirectory.Domain.GetDirectoryEntry()
c#
.net
active-directory
asked on Stack Overflow Oct 29, 2013 by Kenny Lee • edited Nov 24, 2015 by Josh Crozier

1 Answer

1

I've got the same error in case when several of my domain controllers are turned off and became unavailable.

I have changed the way I'm getting the DirectoryEntry instance for the domain:

entry = new DirectoryEntry(string.Format("LDAP://{0}", d.Name), UserNameFull, password);

This works fine because the error "The server is not operational" is raised, so it gets the points to the user that something wrong with its DC.

answered on Stack Overflow Jul 28, 2014 by stukselbax

User contributions licensed under CC BY-SA 3.0