Results 1 to 3 of 3

Thread: Get Computers From Active Directory

Threaded View

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Get Computers From Active Directory

    Since i haven't seen an example like this for computers, i will post one.
    Note you must import system.directoryservices.
    Note that this is a peace of code from a larger project i have.What it actually do is put the computers in a listview but you can modify it to your needs.
    Ok, drag a DirectoryEntry,DirectorySearcher and a listview.

    Code:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' active directory
    
            ListView1.View = View.Details
          
    
    
            Dim sysd As System.DirectoryServices.DirectoryEntry
            Dim form1time As New Form1
          
    
            Try
                sysd = DirectorySearcher1.SearchRoot
            Catch ex As Exception
                MessageBox.Show("Problem with Computer or Active Directory", "Active Directory error", MessageBoxButtons.OK, MessageBoxIcon.Error)
             
                Exit Sub
            End Try
          
    
            Dim path As String
            path = sysd.Path
            ' ' to active directory
            DirectoryEntry1.Path = path
    
            Dim child As System.DirectoryServices.DirectoryEntry
            Dim mySearcher As New System.DirectoryServices.DirectorySearcher(DirectoryEntry1)
            Dim result As System.DirectoryServices.SearchResult
            mySearcher.Filter = ("CN=Computers")
    
            Dim strtest As String
    
            For Each result In mySearcher.FindAll()
                For Each child In result.GetDirectoryEntry.Children
                    strtest = child.Name.Remove(0, 3)
                    Dim itemForActiveDir As New ListViewItem(strtest)
                    itemForActiveDir.UseItemStyleForSubItems = False
                    itemForActiveDir.ImageIndex = 0
                    itemForActiveDir.SubItems.Add(strtest)
                    ListView1.Items.Add(itemForActiveDir)
                Next
    
            Next
    end sub
    Note that this is done with VS2003 but i don't think there is any difference in new versions.
    Last edited by sapator; Mar 14th, 2010 at 09:03 AM.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width