There are some resources on the internet that finally address using .NET and connecting to resources using ADSI. Here are a few links that I recently used for writing an application that will query Active Directory using the .NET and the LDAP provider. As the data is queried, I put the data into an in memory datatable and finally commited to a database. This is my first experience with the LDAP provider, since I really need to get under the hood on dealing with this provider vs the older WinNT provider because of the active directory work i’m doing.. This sample code is something I came up with. It recurses the entire directory starting at my fake domain name called Steve.Schofield.com Check out the MS link listed below, this will give you more examples of how to use the .Filter property. This isn’t anything real hard but thought I’d share, once this is entirely completed I’ll post up what has been completed. This entire exercise querys the Win32_NetworkAdapterConfiguration WMI class. Note this code doesn’t show it but here is the WMI query I’m returning. “SELECT DNSHostName, DefaultIPGateway, DNSServerSearchOrder, DNSDomainSuffixSearchOrder, IPAddress, IPSubnet, MACAddress, WINSPrimaryServer, WINSSecondaryServer FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True” The real hard thing dealing with this Win32 Class is many of these items datatype are string of array.
Imports System.Management
Imports System.DirectoryServices
Imports System.Net
Imports System.Text
Module Module1
Sub Main()
Dim strUID As String = “domainuserid”
Dim strPWD As String = “password”
ListComputers(strUID, strPWD)
End Sub
Sub ListComputers(ByVal strUID As String, ByVal strPWD As String)
Dim strPath As String = “LDAP://DC=Steve,DC=Schofield,DC=com“
Try
Dim objDomain As DirectoryEntry = New DirectoryEntry(strPath)
Dim objOU As DirectoryEntries = objDomain.Children
Dim objOUName As DirectoryEntry
Dim strSubPath As String
For Each objOUName In objOU
strSubPath = objOUName.Name.ToString() & “,DC=Steve,DC=Schofield,DC=com”
GetServerName(strSubPath)
Next
objDomain.Dispose()
Catch f As Exception
Console.WriteLine(f.Message)
End Try
End Sub
Sub GetServerName(ByVal strPath As String)
Try
Dim enTry As DirectoryEntry = New DirectoryEntry(“LDAP://” & strPath)
Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
mySearcher.Filter = “(&(objectClass=computer))”
Dim resEnt As SearchResult
Dim rowcomputer As DataRow
Try
For Each resEnt In mySearcher.FindAll()
Console.Writeline(resEnt.GetDirectoryEntry.Properties.Item(“cn”).Value)
Console.WriteLine(resEnt.GetDirectoryEntry().Path.ToString())
Console.WriteLine(resEnt.GetDirectoryEntry.Properties.Item(“operatingSystem”).Value)
Next
Catch f As Exception
Console.WriteLine(f.Message)
End Try
Catch f As Exception
Console.WriteLine(f.Message)
End Try
End Sub
End Module
Listing All Computers in Active Directory
http://www.vbdotnetheaven.com/Code/May2003/1004.asp
Directory Searcher Class
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdirectoryservicesdirectorysearcherclasstopic.asp
ADSI Scripts resource
http://cwashington.netreach.net/depo/default.asp?topic=adsifaq