Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I determine if the machine I am using is a domain controller

Using C#, what is the way to find out if the machine my code is running on IS a domain controller.

I do not want to "go out" and collect information about any other domain controllers on the domain... I am ONLY interested if the machine machine my code is running on IS a domain controller or not. Additionally, I do not need to know if its a primary domain controller ... just if it IS a domain controller.

Here is what I have tried

TRIAL 1

    private bool IsDomainController()
    {
        Domain domain = Domain.GetCurrentDomain();
        string domainName = domain.ToString();
        bool bIsDC = false;
        DirectoryContext ctx = new DirectoryContext(DirectoryContextType.Domain, domainName);

        try
        {
            using (DomainController dc = DomainController.FindOne(ctx, LocatorOptions.ForceRediscovery))
            {
                bIsDC = true;
            }
        }
        catch (Exception)
        {
            bIsDC = false;              
        }

        return bIsDC;
    }

and

TRIAL 2

    public bool IsThisMachineIsADomainController()
    {
        Domain domain = Domain.GetCurrentDomain();

        string thisMachine = String.Format("{0}.{1}", Environment.MachineName, domain.ToString());
        thisMachine = thisMachine.ToLower();

        //Enumerate Domain Controllers
        List<string> allDcs = new List<string>();

        string name = "";
        foreach (DomainController dc in domain.DomainControllers)
        {
            name = dc.Name.ToLower();
            allDcs.Add(name);
        }
        return allDcs.Contains(thisMachine);
    }

Are either of these suitable and why or why not?

like image 605
MrLister Avatar asked Nov 23 '25 06:11

MrLister


1 Answers

After much research... there are two ways:

Works on Windows 2008 server:

    public bool isActiveDirectoryDomainServicesInstalled()
    {
        bool bRetval = false;
        try
        {
            uint uID = 110;
            string search = string.Format("SELECT * FROM Win32_ServerFeature WHERE ID = {0}", uID);
            ManagementObjectSearcher oSearcher = new ManagementObjectSearcher("root\\CIMV2", search);

            foreach (var oReturn in oSearcher.Get())
            {
                if ((uint)(oReturn["ID"]) == uID)
                {
                    bRetval = true;
                    break;
                }
            }
        }
        catch (Exception)
        {
            bRetval = false;
        }

        return bRetval;
    }

. AND .

Works on Windows 2008 and Windows 2012 servers

public bool IsThisMachineIsADomainController()
{
    Domain domain = Domain.GetCurrentDomain();

    string thisMachine = String.Format("{0}.{1}", Environment.MachineName, domain.ToString());
    thisMachine = thisMachine.ToLower();

    //Enumerate Domain Controllers
    List<string> allDcs = new List<string>();

    string name = "";
    foreach (DomainController dc in domain.DomainControllers)
    {
        name = dc.Name.ToLower();
        allDcs.Add(name);
    }
    return allDcs.Contains(thisMachine);
}
like image 170
MrLister Avatar answered Nov 25 '25 18:11

MrLister



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!