How do I find out which computer is the domain controller in Windows programmatically?

前端 未结 7 878
星月不相逢
星月不相逢 2021-01-30 08:17

I am looking for a way to determine what the Name/IP Address of the domain controller is for a given domain that a client computer is connected to.

At our company we hav

相关标签:
7条回答
  • 2021-01-30 08:25

    With the most simple programming language: DOS batch

    echo %LOGONSERVER%
    
    0 讨论(0)
  • 2021-01-30 08:29

    In cmd on Windows, type the following commande:

    nltest /dclist:{domainname}
    

    It lists all domain controllers in particular domain

    0 讨论(0)
  • 2021-01-30 08:29

    Run gpresult at a Windows command prompt. You'll get an abundance of information about the current domain, current user, user & computer security groups, group policy names, Active Directory Distinguished Name, and so on.

    0 讨论(0)
  • 2021-01-30 08:30

    In C#/.NET 3.5 you could write a little program to do:

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

    This will list all the users in the current domain:

    using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
    {
        using (UserPrincipal searchPrincipal = new UserPrincipal(context))
        {
           using (PrincipalSearcher searcher = new PrincipalSearcher(searchPrincipal))
           {
               foreach (UserPrincipal principal in searcher.FindAll())
               {
                   Console.WriteLine( principal.SamAccountName);
               }
           }
        }
    }
    
    0 讨论(0)
  • 2021-01-30 08:39

    in Powershell: $env:logonserver

    0 讨论(0)
  • 2021-01-30 08:40

    To retrieve the information when the DomainController exists in a Domain in which your machine doesn't belong, you need something more.

      DirectoryContext domainContext =  new DirectoryContext(DirectoryContextType.Domain, "targetDomainName", "validUserInDomain", "validUserPassword");
    
      var domain = System.DirectoryServices.ActiveDirectory.Domain.GetDomain(domainContext);
      var controller = domain.FindDomainController();
    
    0 讨论(0)
提交回复
热议问题