Results 1 to 3 of 3

Thread: Get Computers From Active Directory

  1. #1

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

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Get Computers From Active Directory

    I didnt think you could even use the DirectoryServices assembly in VS2003 - I thought it was .NET 2.0 and above only... but I guess I must be wrong if you are using it with .NET 1/1.1

    Anyway, there's a few issues with your code - the most obvious one being that you are just searching for the default Computers container in AD and then enumerating its children. The problem with this is that a lot of people wont have all of their computer accounts just in one OU or container, so rather than using this as your filter:
    Code:
    mySearcher.Filter = ("CN=Computers")
    A better idea might be to use a filter like this:
    Code:
    mySearcher.Filter = ("objectClass=Computer")
    and then you dont need to enumerate the children of the container because your search results are already all of the computer accounts. So you can do DirectorySearcher.FindAll and just loop through the results returned by that.

    This might be useful for anyone that finds this thread as well, its a control I wrote that lets you display active directory objects in a treeview: http://www.vbforums.com/showthread.php?t=555544
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

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

    Re: Get Computers From Active Directory

    Hi.
    Thanks for the corrections.
    Also may i note that what this do is putting the values on the listview.You won't see anything on the form.The idea is to use the listview values as you like.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

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