-->

List of computers in Active Directory that are onl

2019-04-14 18:19发布

问题:

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?

回答1:

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


回答2:

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.