Custom Search

Friday, November 28, 2008

List the machines on my network Using VB.Net

' 1. connect to your domain controller, binding using currently logged on credentials
Dim root As New DirectoryEntry("LDAP://dc=contoso,dc=local")

' 2. put together an ldap query to retrieve all computer accounts
Dim searcher As New DirectorySearcher(root)
searcher.Filter = "(objectClass=computer)"
searcher.SearchScope = SearchScope.Subtree
searcher.Sort = New SortOption("displayName", SortDirection.Descending)

' 3. execute the query
Dim results As SearchResultCollection = searcher.FindAll()

' 4. loop through the results
For Each computer As SearchResult In results
Console.WriteLine(computer.GetDirectoryEntry().Properties("displayName").Value)
Next

No comments: