2
votes

I'm attempting to get the current domain controller name via c#. This code will NOT be running during a logon session. It runs during machine startup so, I can't use the %logonserver% variable because there is no such thing at machine startup. Searching here I thought the following code would work but it returns the primary domain controller, not the current logon server. (at startup the 'logon server' might be best referred to as the 'authentication server')

this doesn't work for me (doesn't return machines' current DC, returns Domain's PDC)

    using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
        {
            string controller = context.ConnectedServer;
            Console.WriteLine("Domain Controller: " + controller);
        }

I have found that the following WMI query gets positive results but, it's slow:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_NTDomain");

            foreach (ManagementObject queryObj in searcher.Get())
            {
                if (queryObj["DomainControllerName"] != "")
                  Console.WriteLine("DomainControllerName: {0}", queryObj["DomainControllerName"]);
            }

Anyone know a better way?

1
Something to think about. Keep in mind that there may be Multiple domain controllers in a domain, not just one. Ours has two, I think three is ideal as there are three major functions that the domain controllers can do (this is from memory at this point). So, which domain controller do you consider "current"? - Jim
If the machine has not logged on to the domain, as you say "...it runs during a machine startup ... " then neither a user nor the machine has logged into a domain yet, and I believe that the same truth that applies to the user applies to the machine, there is not yet a current domain controller for the machine context either. I could be wrong, but I believe this to be the case. - Cos Callis
Thanks for your reply. I have 15 or 16 DCs in my environment. Some physical locations have 1, some have 3. - Fred B
Still getting used to the edit system here... When a machines starts up and runs startup scripts, it is connected to a particular DC. At that point it's the active authentication server. The machine account is logged on to it. So if you have a machine caled PC123, the system's account PC123$ (or $PC123) is logged on but, it doesn't have a "logonserver" environment variable. The WMI code above exposes the particular server in question. It's slow. I'm just seeking around built in method in c# that might do the trick a bit better. - Fred B

1 Answers

2
votes

Granted you'll need to include a user and password, but this should do the trick:

  DirectoryContext domainContext =  new DirectoryContext(DirectoryContextType.Domain, "targetDomainName", "validUserInDomain", "validUserPassword");

  var domain = System.DirectoryServices.ActiveDirectory.Domain.GetDomain(domainContext);
  var controller = domain.FindDomainController();