Link to home
Start Free TrialLog in
Avatar of Chris Jones
Chris JonesFlag for United States of America

asked on

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.
Avatar of wasiftoor
wasiftoor
Flag of United States of America image

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!
Avatar of Chris Jones

ASKER

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
SOLUTION
Avatar of Paul Jackson
Paul Jackson
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks that works
i have been trying to display all of my groups is there soem code that can help me use these provided functions.
any ideals
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

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i get this eror when i run the above function

Exception has been thrown by the target of an invocation.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i was removed from this project by my job thanks for helping everyone
I've requested that this question be deleted for the following reason:

no longer need help
The question has been answered and further help given points should be awarded
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.