gpt4 book ai didi

c# - 获取 AD 计算机描述

转载 作者:行者123 更新时间:2023-12-04 16:50:01 25 4
gpt4 key购买 nike

我需要获取域计算机的描述。从注册表中我只能获得本地描述,但我需要 Active Directory 描述。有什么想法吗?

期待!谢谢!

最佳答案

使用 DirectoryEntry 类(System.DirectoryServices 命名空间)来连接到事件目录。提供用户名和密码以及 LDAP 根路径在 Active Directory 中搜索计算机对象。然后使用 DirectorySearcher查询计算机对象的类。

下面的代码显示了如何搜索名为 computer01 的计算机。我还将 description 属性添加到要加载的属性中(并非所有属性都默认加载)。在下面的代码中,您必须替换使用您的域 Controller 的名称。同理替换标签用你的域名。例如,如果您的 Active Directory 服务器的名称是server01 并且您的域名是 fabrikam.com 然后 LDAP 路径是 LDAP://server01/dc=fabrikam,dc=com

using (DirectoryEntry entry = new DirectoryEntry("LDAP://<your-ad-server-name>/dc=<domain-name-part>,dc=<domain-name-part>",
"Administrator", "Your Secure Password", AuthenticationTypes.Secure))
{
using (DirectorySearcher adSearcher = new DirectorySearcher(entry))
{
string computerName = "computer01";
adSearcher.Filter = "(&(objectClass=computer)(cn=" + computerName + "))";
adSearcher.SearchScope = SearchScope.Subtree;
adSearcher.PropertiesToLoad.Add("description");
SearchResult searchResult = adSearcher.FindOne();

Console.Out.WriteLine(searchResult.GetDirectoryEntry().Properties["description"].Value);
}
}

请注意,上面的代码在整个 Active Directory 中搜索计算机对象。要仅在Computers 容器 中搜索,请使用以下 LDAP 路径:

LDAP://<your-ad-server-name>/cn=Computers,dc=<domain-name-part>,dc=<domain-name-part>

关于c# - 获取 AD 计算机描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19858203/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com