Home All Groups Group Topic Archive Search About

How to Query AD for Computer Names?

Author
28 Aug 2006 4:43 AM
Terry Olsen
I want to query AD for computer names.

I can do it with using the dsquery command "dsquery computer -name
MTBIL* -limit 0" and parse the computer name out of the response, but I'd
like to know how to do it in VB.NET code.

Thanks.

Author
30 Aug 2006 3:37 PM
Paul Clement
On Sun, 27 Aug 2006 22:43:12 -0600, "Terry Olsen" <tolse***@hotmail.com> wrote:

¤ I want to query AD for computer names.
¤
¤ I can do it with using the dsquery command "dsquery computer -name
¤ MTBIL* -limit 0" and parse the computer name out of the response, but I'd
¤ like to know how to do it in VB.NET code.

See if the following works for you:

        Try
            Dim RootDSE As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
            Dim DomainDN As String = RootDSE.Properties("DefaultNamingContext").Value
            Dim ADEntry As System.DirectoryServices.DirectoryEntry = New
System.DirectoryServices.DirectoryEntry("LDAP://" & DomainDN)
            Dim ADSearcher As System.DirectoryServices.DirectorySearcher = New
System.DirectoryServices.DirectorySearcher(ADEntry)
            ADSearcher.Filter = ("(objectClass=computer)")
            Dim SearchEntry As System.DirectoryServices.SearchResult
            For Each SearchEntry In ADSearcher.FindAll()

                Try
                    Console.WriteLine(":Processing:" &
Mid(SearchEntry.GetDirectoryEntry().Name.ToString(), 4))

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

        Catch

        End Try


Paul
~~~~
Microsoft MVP (Visual Basic)