How to Query AD for Computer Names?

  • Thread starter Thread starter Terry Olsen
  • Start date Start date
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.
 
¤ 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)
 
Back
Top