Comparing Active Directory Group Users

saidev

Active member
Joined
Dec 22, 2005
Messages
27
Programming Experience
1-3
Hi Guys,

I have bunch of users in one of the Active Directory Group. I am adding users from one of my Sql Server table. I want to compare the Active Directory group users with my sql table users and add the users to that group only if they don't exists in that group. here is the code i am adding to the AD Group from sql table. Can you guys help me how to compare?. Thanks for your help

Public Sub adUserToGroup()
Dim sDomainName As String = ("LDAP://servername.fte.fcteg.com")
Dim adUserFolder As DirectoryEntry = New DirectoryEntry("LDAP://Servername.fce.findoe.com/DC=fte,DC=foxeg,DC=com")
Try

adUserFolder.Username = "Name"
adUserFolder.Password = "pass"
Dim adSearch As New System.DirectoryServices.DirectorySearcher(adUserFolder)
adSearch.Filter = String.Format("(&(objectCategory=group)(sAMAccountName= {0}))", "FM Systems")

For Each x As SearchResult In adSearch.FindAll

Dim group As DirectoryEntry = x.GetDirectoryEntry

Dim sql As String

sql = "select ID,DistinguishedName from FMSAD"
Dim myconn As New SqlConnection(CONNECTIONSTRING)
Dim mycommand As New SqlCommand(sql, myconn)
myconn.Open()

Dim dbreader As SqlDataReader = mycommand.ExecuteReader
While dbreader.Read

Dim DistinguishedName As String
If dbreader("DistinguishedName") Is DBNull.Value Then
DistinguishedName = ""
Else
DistinguishedName = dbreader("DistinguishedName")
End If


group.Properties("member").Add(DistinguishedName)

group.CommitChanges()

End While

Next

Catch ex As Exception
End Try
 

Latest posts

Back
Top