How to determine if the "Active Directory Domain Services" role has been installed on a server
Asked Answered
E

2

5

I am trying to figure out if the Active Directory Domain Services are installed a windows server.

I know they show up in the Server Manager, but can I programmatically get if the role is installed on a server using C# code

Eileen answered 29/8, 2014 at 17:17 Comment(3)
Which roles are you asking about? All of them, or only DC?Topple
The Active Directory Domain Services roleEileen
Thanks. I've changed your title to make it clear you don't need a general mechanism for finding about all roles on the server.Topple
F
3

If you know the name of the server you want to test and can run the program with domain admin privileges remotely, you can use WMI:

internal static bool IsDomainController(string ServerName)
{
    StringBuilder Results = new StringBuilder();

    try
    {
        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher("\\\\" + ServerName + "\\root\\CIMV2",
            "SELECT * FROM Win32_ServerFeature WHERE ID = 10");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            Results.AppendLine(queryObj.GetPropertyValue("ID").ToString());
        }
    }
    catch (ManagementException)
    {
        //handle exception
    }

    if (Results.Length > 0)
        return true;
    else
        return false;
}

If you're running that locally on the server, the WMI path changes to:

        ManagementObjectSearcher searcher =
            new ManagementObjectSearcher("root\\CIMV2",
            "SELECT * FROM Win32_ServerFeature WHERE ID = 10");

See the MSDN reference on Win32_ServerFeature for a full list of roles and their ID numbers.

Funke answered 29/8, 2014 at 22:2 Comment(0)
C
2

If your question is to see if a server is a domain controller, you can enumerate the domain controllers in the domain and check the hostname of the server you are sitting on to see if it matches any of them. To get the list of domain controllers:

        var domainControllers = new List<string>();
        var domain = Domain.GetCurrentDomain();
        foreach (var dc in domain.DomainControllers)
        {
            domainControllers.Add(dc.Name);
        }
        string whoami = Dns.GetHostname();

Make sure to add requisite error handling (like if you run this on a workgroup computer, it will die).

EDIT: Alternate ways of detecting DCPROMO (because it's possible to install Domain Services without DCPROMO, and that is a bad thing):

1) Parse out (and check for the existence of) the debug log that is created when DCPROMO does its thing. Should be located at c:\windows\debug\dcpromo.log

2) This DSQUERY command is FAST and will give you all the servers where DCPROMO was ran:

 dsquery * "cn=Sites,cn=Configuration,dc=MyDomain,dc=com" -Filter "(cn=NTDS Settings)" -attr distinguishedName whenCreated

Problem is getting that from command line output if you started it using Process. Working on a way to do this and will update once I have it tested, as I haven't done AD filtering in a query for a while.

Cecilia answered 29/8, 2014 at 21:19 Comment(1)
This can take a LONG time if you have a lot of domain controller in the domain and if they are spread out across the world. Need something that is very quick... and only for the computer the program is running on.Eileen

© 2022 - 2024 — McMap. All rights reserved.