Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 341
MrLister Avatar asked Aug 29 '14 17:08

MrLister


People also ask

How do I know if an AD is installed on my server?

Thoroughly test the domain controller for all directory service issues, you can run the dcdiag /v command. The output of this command provides detailed information about the conditions on the domain controller. SysVol folder will be displayed if the Active Directory is installed.

How do I find Active Directory domain Services?

Right-click the root domain, and click Properties. Under the General tab, you will find the forest and domain functional levels currently configured on your Active Directory Domain Controller.

Which role is automatically installed on domain controllers?

Operations master role holders are assigned automatically when the first domain controller in a given domain is created.

What is Active Directory domain Services in server?

What is Active Directory Domain Services? Active Directory Domain Services (AD DS) is a server role in Active Directory that allows admins to manage and store information about resources from a network, as well as application data, in a distributed database.


1 Answers

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.

like image 105
raney Avatar answered Oct 29 '22 16:10

raney