Active Directory中联机的计算机列表

时间:2010-09-23 13:04:03

标签: c# active-directory ldap jscript.net

我正在使用这段代码输出网络上所有计算机的列表(语言是jscript.net,但它只是对C#的一个小操作)。

    var parentEntry = new DirectoryEntry();

    parentEntry.Path = "WinNT:";
    for(var childEntry in parentEntry.Children) {
        if(childEntry.SchemaClassName == "Domain") {
            var parentDomain = new TreeNode(childEntry.Name); 
            this.treeView1.Nodes.Add(parentDomain);

            var subChildEntry : DirectoryEntry;
            var subParentEntry = new DirectoryEntry();
            subParentEntry.Path = "WinNT://" + childEntry.Name;
            for(subChildEntry in subParentEntry.Children) {
                var newNode1 = new TreeNode(subChildEntry.Name);
                if(subChildEntry.SchemaClassName == "Computer") {
                    parentDomain.Nodes.Add(newNode1);
                }
            }
        }

    }

我有两个问题:

1)非常慢。大约有100台计算机显示,加载大约需要1分钟。

2)我想只获得当前在线的计算机列表。

这可以做到,因为我已经看到其他程序在做这件事并且速度更快,而且他们只能在线显示这些程序。

我错过了什么吗?

2 个答案:

答案 0 :(得分:3)

我知道它有点旧,但对于其他人......这个片段将在2-3秒内使用AD从域中返回760个计算机名称....显着改进....享受!

    Friend Shared Function DomainComputers() As Generic.List(Of String)

    ' Add a Reference to System.DirectoryServices

    Dim Result As New Generic.List(Of String)

    Dim ComputerName As String = Nothing
    Dim SRC As SearchResultCollection = Nothing
    Dim Searcher As DirectorySearcher = Nothing

    Try

        Searcher = New DirectorySearcher("(objectCategory=computer)", {"Name"})

        Searcher.Sort = New SortOption("Name", SortDirection.Ascending)
        Searcher.Tombstone = False

        SRC = Searcher.FindAll

        For Each Item As SearchResult In SRC
            ComputerName = Item.Properties("Name")(0)
            Result.Add(ComputerName)
        Next

    Catch ex As Exception

    End Try

    Return Result

End Function

答案 1 :(得分:0)

我会查看CodePlex上的Linq To Active Directory

您还必须定义“我的网络”。你的子网?你的组织单位?你的域名?你的森林?

还要考虑您要查询的LDAP服务器的位置。是关闭还是在远程链接的另一端?

你还认为“在线”是什么?你期望能够ping它吗?您希望能够连接到它并执行操作吗?

这里有很多事情需要考虑。此外,如果您有其他基础架构组件(如SCCM / SMS服务器),则通常可以更快地查询它们,因为所有发现数据都已流入数据仓库。

相关问题