NeoGenesis521
NeoGenesis521

Reputation: 43

How to get information about Domain Controller, status and Replication status using LDAP and C#?

I am trying to use LDAP to get information about domain controllers and to know about its status and Replication status using C#. I am trying to do this approach remotely; for example, I am running code in PC1 and would like to know and get information from PC2 .

I know when using WMI, it is possible to get information using the following queries:

SELECT * FROM MSAD_DomainController
then
SELECT * FROM MSAD_ReplCursor where SourceDsaDN like '%{domainControllerName}%'
then
SELECT * FROM MSAD_ReplNeighbor where NamingContextDN like '{NamingContextDN->From above query}'

So what would I need to get information similar to the WMI query when using LDAP?

I am using this code to look into the details of domain controller

using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

class Program
{
    static void Main()
    {
        string domainname = {Domain}; 
        string username = {User};
        string password = {Pass};

        try
        {
            // Using DirectoryEntry for LDAP operations
            string ipAddress = {IP};
            string ldapPath = $"LDAP://{ipAddress}:389/DC={domainname},DC=com";
            using (DirectoryEntry entry = new DirectoryEntry(ldapPath, username, password, AuthenticationTypes.Secure))
            {
                using (DirectorySearcher searcher = new DirectorySearcher(entry))
                {
                    searcher.Filter = "(objectClass=*)"; 
                    searcher.SearchScope = SearchScope.Subtree; 

                    SearchResultCollection results = searcher.FindAll();

                    if (results.Count == 0)
                    {
                        Console.WriteLine("No objects found.");
                    }
                    else
                    {
                        foreach (SearchResult result in results)
                        {
                            Console.WriteLine("Object Information:");
                            
                            if (result.Properties["objectClass"].Count > 0)
                            {
                                Console.Write($"Object Class: ");

                                foreach (var value in result.Properties["objectClass"])
                                {
                                    Console.Write($"{value}, "); // Print each value
                                }
                            }

                            foreach (string propertyName in result.Properties.PropertyNames)
                            {
                                var propertyValues = result.Properties[propertyName];

                                if (propertyValues.Count > 0)
                                {
                                    Console.Write($"{propertyName}: ");

                                    foreach (var value in propertyValues)
                                    {
                                        Console.Write($"{value}, "); // Print each value
                                    }

                                    Console.WriteLine(); // New line after values
                                }
                                else
                                {
                                    Console.WriteLine($"{propertyName}: Not Available or Empty");
                                }
                            }
                            Console.WriteLine("---------------------------------------------------");
                        }
                    }
                }
            }
        }
        catch (ActiveDirectoryObjectNotFoundException ex)
        {
            Console.WriteLine($"Error: {ex.Message} - Check domain name and connectivity.");
        }
        catch (ActiveDirectoryOperationException ex)
        {
            Console.WriteLine($"AD Operation Error: {ex.Message}");
        }
        catch (DirectoryServicesCOMException ex)
        {
            Console.WriteLine($"Directory Services Error: {ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Unexpected Error: {ex.Message}");
        }
    }
}

Upvotes: 0

Views: 52

Answers (0)

Related Questions