How to Query AD for Computer Names?

T

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.
 
P

Paul Clement

¤ 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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top