【问题标题】:C# Active Directory SearchC# 活动目录搜索
【发布时间】:2020-04-04 21:44:00
【问题描述】:

我有这个 powershell 函数,我想把它变成一个 C# 函数。 我怎样才能把它放到 C# 中?

Get-ADComputer -filter {Name -Like 'myComp'} -property * | select DistinguishedName

【问题讨论】:

标签: c# powershell active-directory


【解决方案1】:

您应该能够很容易地做到这一点。添加对System.DirectoryServices.AccountManagement 的引用,然后使用此代码:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, 'YourDomain'))
{
    ComputerPrincipal computer = ComputerPrincipal.FindByIdentity (ctx, "name");

    if (computer != null)
    {
        // do whatever you need to do with your computer principal
        string distinguishedName = computer.DistinguishedName;
    }

}

更新:如果您不知道您的域........ - 您也可以使用:

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))

在这种情况下,将为您所在的当前域创建主体上下文。

【讨论】:

  • 我不知道我的域名。 Powershell 代码不这样做,只是过滤所有计算机名并选择需要的计算机名,没有域名、服务器名等。
  • string domainName = System.Environment.GetEnvironmentVariable("USERDNSDOMAIN");
  • 谢谢!这成功了。你知道我不知道的域名是从DC1.DC2.DC3.DC4创建的吗?
【解决方案2】:

您可以通过以下方式使用C#

  1. 连接到域控制器并获取 DomainContext
  2. 使用它根据名称查找计算机对象。
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, Environment.UserDomainName) 
{
    using (PrincipalSearcher srch = new PrincipalSearcher(new ComputerPrincipal(ctx) { Name = "ServerName"}))
    {
        return srch.FindAll().Cast<ComputerPrincipal>().ToList().Select(x => x.DistinguishedName);
    }
}

Above 返回与服务器名称匹配的 DistinguishedNames 列表。

【讨论】:

  • 没有域控制器
  • 您可以使用 Environment.UserDomainName 获取当前域并使用它来查找计算机。
【解决方案3】:

所有其他答案都建议使用 System.DirectoryServices.AccountManagement 命名空间。虽然这会起作用,但它实际上只是 System.DirectoryServices 命名空间的包装器,以使事情更易于使用。它确实(有时)使事情变得更容易,但这样做是以牺牲性能为代价的。

例如,在您给出的所有示例中,您的代码将从 AD 中的计算机对象中检索 每个具有值的属性,即使您只需要一个属性。

如果你使用DirectorySearcher,你可以只搜索和检索你想要的那个属性:

public string GetComputerDn(string computerName) {
    var searcher = new DirectorySearcher {
        Filter = $"(&(objectClass=computer)(sAMAccountName={computerName}$))",
        PropertiesToLoad = { "distinguishedName" } //return only the distinguishedName attribute
    };
    var result = searcher.FindOne();
    if (result == null) return null;
    return (string) result.Properties["distinguishedName"][0];
}

请注意,在 AD 中,计算机对象的 sAMAccountName 是您通常所说的“计算机名称”,然后是 $,这就是过滤器的原因。

【讨论】:

    【解决方案4】:

    请试试这个:

    添加对 Active Directory 服务的引用 (%programfiles%\Reference Assemblies\Microsoft\Framework.NETFramework\\System.DirectoryServices.AccountManagement.dll)

    public string GetComputerName(string computerName)
    {
        using (var context = new PrincipalContext(ContextType.Domain, "your domain name goes here"))
        {
            using (var group = GroupPrincipal.FindByIdentity(context, "Active Directory Group Name goes here"))
            {
                var computers = @group.GetMembers(true);
                return computers.FirstOrDefault(c => c.Name == computerName).DistinguishedName;
            }
        }
    
        return null; // or return "Not Found"
    }
    

    【讨论】:

    • OP 正在尝试获取 computer 对象 - 而不是组 ....
    • 这是另一种检索方式。我试过了,它适用于我的情况。
    • GroupPrincipal.FindByIdentity 找不到计算机。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多