Active Directory 中在线的计算机列表

发布于 2024-09-24 11:44:07 字数 1038 浏览 1 评论 0原文

我使用这段代码输出网络上所有计算机的列表(语言是 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) 我只想获取当前在线的计算机列表。

这是可以做到的,因为我见过其他程序这样做,而且它们速度更快,而且它们只能显示在线的程序。

我错过了什么吗?

I'm using this snippet of code to output a list of all the computers on my network (the language is jscript.net, but it's just a small manipulation of 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);
                }
            }
        }

    }

I have 2 issues with this:

1) It is extremely slow. There's about 100 computers showing, and it takes about 1 minute to load.

2) I want to get only a list of computers that are currently online.

This can be done because I've seen other programs doing it and they are much faster, also they're able to show only the ones online.

Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

(り薆情海 2024-10-01 11:44:07

我知道它有点旧,但对其他人来说......这个片段将在 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

I know it's a bit old, but for others...this snippet will return a 760 computer names from a domain using AD within 2-3 seconds....a significant improvement....enjoy!

    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
冬天旳寂寞 2024-10-01 11:44:07

我会查看 CodePlex 上找到的 Linq To Active Directory

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

还要考虑您正在查询的 LDAP 服务器所在的位置。它是在附近还是在远程链接的另一端?

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

这里有很多事情需要考虑。此外,如果您有其他基础设施组件(例如 SCCM / SMS 服务器),那么它们的查询速度通常会更快,因为所有发现数据都已流入数据仓库。

I would look at Linq To Active Directory found on CodePlex

You'd also have to define "my network". Your subnet? Your Organizational Unit? Your domain? Your forest?

Also consider where your LDAP server is that you are querying. Is it close or is it on the other end of a remote link?

Also what do you consider "online"? Do you expect to be able to ping it? Do you expect to be able to connect to it and perform an operation?

There are many things to consider here. Also if you have other infrastructure components such as an SCCM / SMS server they can often be queried much faster since all of the discovery data has flowed up into the data warehouse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文