AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=801&pId=-1
CodeSnip: Get a complete computer list from Active Directory using .NET 2.0
page
by Web Team at ORCS Web
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 15289/ 14

 

This code sample shows how to retrieve a list of computers from Active Directory and use a For/Next to loop through the collection.  I have used this code sample on several scripts.  Using Active Directory as the authoritative source for a list of machines is very handy.  The process running this code sample normally requires a domain administrator level to retrieve this information.   

Note:   The "LDAP://DC=Steve,DC=Schofield,DC=com" is the LDAP path for the domain I am connecting to.  This represents an LDAP-based path to the Active Directory domain you are connecting to.   You will have to change the code sample to what domain you are connecting to. 

 

Code Listing 1:

   Try
    Dim enTry As System.DirectoryServices.DirectoryEntry = _
        New System.DirectoryServices.DirectoryEntry("LDAP://DC=Steve,DC=Schofield,DC=com")
    Dim mySearcherAs System.DirectoryServices.DirectorySearcher = _
        New System.DirectoryServices.DirectorySearcher(enTry)
    mySearcher.Filter = ("(objectClass=computer)")
    Dim resEntAs System.DirectoryServices.SearchResult
    For Each resEnt In mySearcher.FindAll()

      Try
                    'The reason for using the MID statement is when retrievingcomputers
                    'with this method, they are formated CN=COMPUTERNAME.   TheMID statement
                    'parses out just theComputerName.  'CN' stands for 'common name', this is 
                    'the way LDAP stores the computer variable. 
        Console.WriteLine(":Processing:" &Mid(resEnt.GetDirectoryEntry().Name.ToString(), 4))

      Catch ex As Exception
          Console.WriteLine("Trying to Connect to: " & _
            resEnt.GetDirectoryEntry().Name.ToString() & vbCrLf &ex.Message.ToString())
      End Try
    Next

   Catch
   End Try

Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 2:13:11 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search