Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 953
  • Last Modified:

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.
0
Chris Jones
Asked:
Chris Jones
3 Solutions
 
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 JonesAuthor 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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Paul JacksonCommented:
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 JonesAuthor Commented:
thanks that works
0
 
Chris JonesAuthor 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 JonesAuthor Commented:
any ideals
0
 
Paul JacksonCommented:
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 JonesAuthor 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
 
Chris JonesAuthor Commented:
i get this eror when i run the above function

Exception has been thrown by the target of an invocation.
0
 
Paul JacksonCommented:
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 JonesAuthor Commented:
i was removed from this project by my job thanks for helping everyone
0
 
Chris JonesAuthor Commented:
I've requested that this question be deleted for the following reason:

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

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Tackle projects and never again get stuck behind a technical roadblock.
Join Now