connect to AD in vb.net and update AD

Hello,

i have a project that i am having a hard time with i need to be able to read any ad user from our server and update there records as well as add them and remove them from groups.
LVL 1
Chris JonesLead Application Web DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wasiftoorCommented:
There is an excellent article on codeproject.com that demonstrates exactly what you are looking for including:

Add a new user
Suspend a user's account
Enable a user's account
Reset a user's password
Update a user account
Add a user to a specific group
Remove a user from a group
Retrieve the list of all groups a user is a member of
Retrieve all computers connected to the network
Determine if a user's account is disabled
Check if a user account is active (perform a basic login)


Please refer to http://www.codeproject.com/Articles/19689/Working-with-Active-Directory-in-VB-NET. Hopefully this will be helpful.

Good Luck!
0
Chris JonesLead Application Web DeveloperAuthor Commented:
i get a few errorsd in my function that i got from the first post

    ''' <summary>
    ''' Method that updates user's properties
    ''' </summary>
    ''' <param name="userLogin">Login of the user to update</param>
    ''' <param name="userDepartment">New department of the specified user</param>
    ''' <param name="userTitle">New title of the specified user</param>
    ''' <param name="userPhoneExt">New phone extension of the specified user</param>
    Public Sub UpdateUserADAccount(ByVal userLogin As String, ByVal userDepartment As String, ByVal userTitle As String, ByVal userPhoneExt As String)
        Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
        '   1. Search the Active Directory for the speied user
        dirSearcher.Filter = "(&(objectCategory=Person)(objectClass=user) (SAMAccountName=" & userLogin & "))"
        dirSearcher.SearchScope = SearchScope.Subtree
        Dim searchResults As SearchResult = dirSearcher.FindOne()
        If Not searchResults Is Nothing Then
            Dim dirEntryResults As New DirectoryEntry(results.Path)
            'The properties listed here may be different then the
            'properties in your Active Directory so they may need to be
            'changed according to your network
            '   2. Set the new property values for the specified user
            SetProperty(dirEntryResults, "department", userDepartment)
            SetProperty(dirEntryResults, "title", userTitle)
            SetProperty(dirEntryResults, "phone", userPhoneExt)
            '   3. Commit the changes
            dirEntryResults.CommitChanges()
            '   4. Close & Cleanup
            dirEntryResults.Close()
        End If
        '   4a. Close & Cleanup
        dirEntry.Close()
    End Sub


ERRORS
Error      4      'results' is not declared. It may be inaccessible due to its protection level.      C:\Code\PartalGroups\PartalGroups\LDAP.vb      120      55      PartalGroups
Error      5      'SetProperty' is not declared. It may be inaccessible due to its protection level.      C:\Code\PartalGroups\PartalGroups\LDAP.vb      125      13      PartalGroups
Error      6      'SetProperty' is not declared. It may be inaccessible due to its protection level.      C:\Code\PartalGroups\PartalGroups\LDAP.vb      126      13      PartalGroups
Error      7      'SetProperty' is not declared. It may be inaccessible due to its protection level.      C:\Code\PartalGroups\PartalGroups\LDAP.vb      127      13      PartalGroups
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Paul JacksonSoftware EngineerCommented:
Looks like the articles has a few bugs in it.

results.Path on line 120 should be searchResults.path

setProperty should be SetADProperty which is the helper method at the start of the article so make sure you have that in your code as well.
0
Chris JonesLead Application Web DeveloperAuthor Commented:
thanks that works
0
Chris JonesLead Application Web DeveloperAuthor Commented:
i have been trying to display all of my groups is there soem code that can help me use these provided functions.
0
Chris JonesLead Application Web DeveloperAuthor Commented:
any ideals
0
Paul JacksonSoftware EngineerCommented:
try. :
Public Function GetAllADGroups() As List(Of String)
 
    Try

        Dim myDirectory As DirectoryEntry = GetDirectoryEntry()
        Dim mySearcher As New DirectorySearcher(myDirectory)
        Dim mySearchResultColl As SearchResultCollection
        Dim mySearchResult As SearchResult
        Dim result As New List(Of String)
        Dim objGroupEntry As DirectoryEntry
 
        ' Build LDAP query
        mySearcher.Filter = "(&(objectClass=group))"
        mySearchResultColl = mySearcher.FindAll()
 
        ' enumerate
        If (mySearchResultColl.Count <> 0) Then
            For Each mySearchResult In mySearchResultColl
                objGroupEntry = mySearchResult.GetDirectoryEntry()
                result.Add(objGroupEntry.Name)
            Next
        End If
 
        ' return value
        Return result
 
    Catch ex As System.Exception
        Return Nothing
    End Try
 
End Function

Open in new window

0
Chris JonesLead Application Web DeveloperAuthor Commented:
Hello in this function what does the de and the deUser mean

''' <summary>
''' Method to add a user to a group
''' </summary>
''' <param name="de"></param>
''' <param name="deUser"></param>
''' <param name="GroupName"></param>
Public Shared Sub AddUserToGroup(ByVal de As DirectoryEntry, ByVal deUser As DirectoryEntry, ByVal GroupName As String)
Dim deSearch As DirectorySearcher = New DirectorySearcher()
deSearch.SearchRoot = de
deSearch.Filter = "(&(objectClass=group) (cn=" & GroupName & "))"
Dim results As SearchResultCollection = deSearch.FindAll()
Dim isGroupMember As Boolean = False
If results.Count>0 Then
Dim group As DirectoryEntry = GetDirectoryEntry(results(0).Path)
Dim members As Object = group.Invoke("Members",Nothing)
For Each member As Object In CType(members, IEnumerable)
Dim x As DirectoryEntry = New DirectoryEntry(member)
Dim name As String = x.Name
If name <> deUser.Name Then
isGroupMember = False
Else
isGroupMember = True
Exit For
End If
Next member
If (Not isGroupMember) Then
group.Invoke("Add", New Object() {deUser.Path.ToString()})
End If
group.Close()
End If
Return
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris JonesLead Application Web DeveloperAuthor Commented:
i get this eror when i run the above function

Exception has been thrown by the target of an invocation.
0
Paul JacksonSoftware EngineerCommented:
de is the root Active Directory directory entry to use for the search of the group a call to the GetDirectoryEntry() function should be used to initialize this value.

deUser is the user you want to add to the group a call to the first GetUser() function in the article will get you the correct value for this.

The error you are getting is probably related to an incorrect value for de.
0
Chris JonesLead Application Web DeveloperAuthor Commented:
i was removed from this project by my job thanks for helping everyone
0
Chris JonesLead Application Web DeveloperAuthor Commented:
I've requested that this question be deleted for the following reason:

no longer need help
0
Paul JacksonSoftware EngineerCommented:
The question has been answered and further help given points should be awarded
0
Paul JacksonSoftware EngineerCommented:
3) Accept one or more Expert posts as the answer

100 points : http:#37812604

300 points :  http:#37823187

100 points : http:#37825807

The reason I have assigned points this way is these expert responses are the only ones that attempted to help the user, and posting links without following up should be discouraged.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.