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.
Bill sambrone
source share