列出活动目录中的所有计算机

EKS*_*EKS 13 c# ldap active-directory

我想知道如何从活动目录中获取所有计算机/机器/ PC的列表?

(试图让这个页面成为搜索引擎诱饵,会回复自己.如果有人有更好的回复,我会接受)

EKS*_*EKS 26

如果您有一个非常大的域,或者您的域在每次搜索可以返回多少项的配置限制,您可能必须使用分页.

using System.DirectoryServices;  //add to references

public static List<string> GetComputers()
{
    List<string> ComputerNames = new List<string>();

    DirectoryEntry entry = new DirectoryEntry("LDAP://YourActiveDirectoryDomain.no");
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = ("(objectClass=computer)");
    mySearcher.SizeLimit = int.MaxValue;
    mySearcher.PageSize = int.MaxValue;

    foreach(SearchResult resEnt in mySearcher.FindAll())
    {
        //"CN=SGSVG007DC"
        string ComputerName = resEnt.GetDirectoryEntry().Name;
        if (ComputerName.StartsWith("CN="))
            ComputerName = ComputerName.Remove(0,"CN=".Length);
        ComputerNames.Add(ComputerName);
    }

    mySearcher.Dispose();
    entry.Dispose();

    return ComputerNames;
}
Run Code Online (Sandbox Code Playgroud)


qua*_*oft 12

EKS建议的是正确的,但表现得有点.

原因是GetDirectoryEntry()对每个结果的调用.这将创建一个DirectoryEntry对象,只有在需要修改活动目录(AD)对象时才需要该对象.如果您的查询将返回单个对象,但是在AD中列出所有对象时,这会很好地降低性能.

如果您只需要查询AD,最好只使用Properties结果对象的集合.这将多次提高代码的性能.

课程文档中SearchResult对此进行了解释:

该实例SearchResult类非常相似的实例 DirectoryEntry类.关键的区别在于,DirectoryEntry每次访问新对象时, 类都会从Active Directory域服务层次结构中检索其信息,而数据中的数据SearchResult已经可用于SearchResultCollection从使用DirectorySearcher该类执行的查询返回的数据.

以下是如何使用该集合的示例Properties:

public static List<string> GetComputers()
{
    List<string> computerNames = new List<string>();

    using (DirectoryEntry entry = new DirectoryEntry("LDAP://YourActiveDirectoryDomain.no")) {
        using (DirectorySearcher mySearcher = new DirectorySearcher(entry)) {
            mySearcher.Filter = ("(objectClass=computer)");

            // No size limit, reads all objects
            mySearcher.SizeLimit = 0;

            // Read data in pages of 250 objects. Make sure this value is below the limit configured in your AD domain (if there is a limit)
            mySearcher.PageSize = 250; 

            // Let searcher know which properties are going to be used, and only load those
            mySearcher.PropertiesToLoad.Add("name");

            foreach(SearchResult resEnt in mySearcher.FindAll())
            {
                // Note: Properties can contain multiple values.
                if (resEnt.Properties["name"].Count > 0)
                {
                    string computerName = (string)resEnt.Properties["name"][0];
                    computerNames.Add(computerName);
                }
            }
        }
    }

    return computerNames;
}
Run Code Online (Sandbox Code Playgroud)

文档 SearchResult.Properties

请注意,属性可以有多个值,这就是我们Properties["name"].Count用来检查值的原因.

为了进一步改进,使用该PropertiesToLoad集合让搜索者知道您将提前使用哪些属性.这允许搜索者仅读取实际将要使用的数据.

请注意,应正确处理DirectoryEntryDirectorySearcher对象以释放所有使用的资源.最好用一个using条款完成.