如何以编程方式找出哪台计算机是 Windows 中的域控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/354037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:50:25  来源:igfitidea点击:

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

windowsnetworking

提问by Dorky Engineer

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.

我正在寻找一种方法来确定客户端计算机连接到的给定域的域控制器的名称/IP 地址。

At our company we have a lot of small little networks that we use for testing and most of them have their own little domains. As an example, one of the domains is named "TESTLAB". I have an Windows XP workstation that is a member of the TESTLAB domain and I am trying to figure out the name of the domain controller so that I can go and look to see what users have been defined for the domain. In our lab there is a mix of Windows Server 2000 and Windows Server 2003 (and in reality probably a couple of NT 4 Servers) so it would be nice to find a solution that would work for both.

在我们公司,我们有很多用于测试的小型网络,其中大多数都有自己的小域。例如,域之一被命名为“TESTLAB”。我有一个 Windows XP 工作站,它是 TESTLAB 域的成员,我正在尝试找出域控制器的名称,以便我可以查看为该域定义了哪些用户。在我们的实验室中,混合使用了 Windows Server 2000 和 Windows Server 2003(实际上可能是几个 NT 4 服务器),因此最好能找到一种适用于两者的解决方案。

Looking on the Internet, it looks like there are various utilities, such as Windows Power Shell or nltest, but these all require that you download and install other utilities. I was hoping to find a way to find the domain controller without having to install anything additional.

在网上看,好像有各种实用程序,比如Windows Power Shell 或者nltest,但是这些都需要你下载安装其他实用程序。我希望找到一种无需安装任何其他东西即可找到域控制器的方法。

EDITIf I wanted to write a program to find the domain controller or the users in the current domain, how would I go about doing that?

编辑如果我想编写一个程序来查找域控制器或当前域中的用户,我将如何去做?

回答by MZywitza

With the most simple programming language: DOS batch

用最简单的编程语言:DOS批处理

echo %LOGONSERVER%

回答by Lado Morela

In cmd on Windows, type the following commande:

在 Windows 上的 cmd 中,键入以下命令:

nltest /dclist:{domainname}

It lists all domain controllers in particular domain

它列出了特定域中的所有域控制器

回答by tvanfosson

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

在 C#/.NET 3.5 中,您可以编写一个小程序来执行以下操作:

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);
           }
       }
    }
}

回答by Sri

From command line query the logonserver env variable.

从命令行查询 loginserver 环境变量。

C:> SET L

C:> 设置 L

LOGONSERVER='\'\DCNAME

登录服务器='\'\DCNAME

回答by ErikE

Run gpresultat 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.

gpresult在 Windows 命令提示符下运行。您将获得有关当前域、当前用户、用户和计算机安全组、组策略名称、Active Directory 专有名称等的大量信息。

回答by Wim

in Powershell: $env:logonserver

在 Powershell 中: $env:logonserver

回答by Brett Veenstra

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

DomainController存在于您的机器不属于的域中时,要检索信息,您需要更多的东西。

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

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