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

I am trying to find out if a Windows server is installed in Active Directory Domain Services.

I know that they are displayed in the server manager, but I can get it programmatically if the role is installed on the server using C # code

+5
c # roles
source share
2 answers

If you know the name of the server you want to test, and you can remotely run the program as a domain administrator, 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 use 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 link on Win32_ServerFeature for a complete list of roles and their ID numbers.

+3
source share

If you ask whether the server is a domain controller, you can list the domain controllers in the domain and check the host name of the server you are sitting on to see if it matches any of them. To get a 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(); 

Be sure to add the required error handling (for example, if you run it on the workgroup computer, it will die).

EDIT: Alternative ways to detect DCPROMO (since it is possible to install domain services without DCPROMO, and this is bad):

1) Parse (and check for) the debug log that is created when DCPROMO does its job. Must be located in c: \ windows \ debug \ dcpromo.log

2) This command is DSQUERY FAST and will give you all the servers on which DCPROMO was running:

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

The problem arises from the output of the command line if you started it using Process. Working on how to do this, and will be updated after I tested it, since I did not perform AD filtering in the request for some time.

+2
source share

All Articles