locked
searching Active Directory RRS feed

  • Question

  • Using VB.net im after a way of serching through active directory and returning the ou a computer is currently in.

    Tuesday, August 16, 2011 11:37 AM

Answers

  • The below code will find the computer object (you will need to change the cn argument in the filter) if available. You can parse the parent OU from the distinguishedName:

        Try
          Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
          Dim DomainDistinguishedName As String = RootDSE.Properties("DefaultNamingContext").Value
          Dim ADEntry As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry("LDAP://" & DomainDistinguishedName)
          Dim ADSearcher As System.DirectoryServices.DirectorySearcher = New System.DirectoryServices.DirectorySearcher(ADEntry)
          ADSearcher.Filter = "(&(objectClass=computer)(cn=computername))"
          Dim ADComputerEntry As System.DirectoryServices.SearchResult
          For Each ADComputerEntry In ADSearcher.FindAll()
            Try
              Console.WriteLine("Computer Name: " & ADComputerEntry.Properties.Item("name")(0).ToString)
              Console.WriteLine("Distinguished Name: " & ADComputerEntry.Properties.Item("distinguishedName")(0).ToString)
            Catch ex As Exception
              Console.WriteLine("Trying to Connect to: " & _
               ADComputerEntry.GetDirectoryEntry().Name.ToString() & vbCrLf & ex.Message.ToString())
            End Try
          Next
        Catch
        End Try
    

     


    Paul ~~~~ Microsoft MVP (Visual Basic)
    • Marked as answer by l.wright1981 Saturday, August 20, 2011 1:57 PM
    Tuesday, August 16, 2011 4:36 PM

All replies

  • Searching Folder

     ListBox2.Items.AddRange(Directory.GetFiles( "C:\Folder\", "*.txt", SearchOption.AllDirectories))
    


    Search Drive and all folders in it

    http://social.msdn.microsoft.com/Forums/en/Vsexpressvb/thread/255be857-9d1e-4c80-9ae2-5c8b48697943


    Please mark those posts as answer which answers your question. Faraz
    Tuesday, August 16, 2011 12:17 PM
  • its Active Directory (LDAP) i which to search through for a computer and have it return the OU its currently in.
    Tuesday, August 16, 2011 12:21 PM
  • The below code will find the computer object (you will need to change the cn argument in the filter) if available. You can parse the parent OU from the distinguishedName:

        Try
          Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
          Dim DomainDistinguishedName As String = RootDSE.Properties("DefaultNamingContext").Value
          Dim ADEntry As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry("LDAP://" & DomainDistinguishedName)
          Dim ADSearcher As System.DirectoryServices.DirectorySearcher = New System.DirectoryServices.DirectorySearcher(ADEntry)
          ADSearcher.Filter = "(&(objectClass=computer)(cn=computername))"
          Dim ADComputerEntry As System.DirectoryServices.SearchResult
          For Each ADComputerEntry In ADSearcher.FindAll()
            Try
              Console.WriteLine("Computer Name: " & ADComputerEntry.Properties.Item("name")(0).ToString)
              Console.WriteLine("Distinguished Name: " & ADComputerEntry.Properties.Item("distinguishedName")(0).ToString)
            Catch ex As Exception
              Console.WriteLine("Trying to Connect to: " & _
               ADComputerEntry.GetDirectoryEntry().Name.ToString() & vbCrLf & ex.Message.ToString())
            End Try
          Next
        Catch
        End Try
    

     


    Paul ~~~~ Microsoft MVP (Visual Basic)
    • Marked as answer by l.wright1981 Saturday, August 20, 2011 1:57 PM
    Tuesday, August 16, 2011 4:36 PM