Solved

connect to AD in  vb.net and update AD

Posted on 2012-04-05
19
913 Views
Last Modified: 2012-05-19
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
Comment
Question by:Chris Jones
19 Comments
 
LVL 9

Expert Comment

by:wasiftoor
ID: 37810473
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37811651
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 37811949
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
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 500 total points
ID: 37812604
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
 
LVL 1

Author Comment

by:Chris Jones
ID: 37812630
thanks that works
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 37814128
i have been trying to display all of my groups is there soem code that can help me use these provided functions.
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 37817028
any ideals
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37817961
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
 
LVL 1

Accepted Solution

by:
Chris Jones earned 0 total points
ID: 37823187
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
 
LVL 1

Author Comment

by:Chris Jones
ID: 37823198
i get this eror when i run the above function

Exception has been thrown by the target of an invocation.
0
 
LVL 29

Assisted Solution

by:Paul Jackson
Paul Jackson earned 500 total points
ID: 37825807
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
 
LVL 1

Author Comment

by:Chris Jones
ID: 37916318
i was removed from this project by my job thanks for helping everyone
0
 
LVL 1

Author Comment

by:Chris Jones
ID: 37916341
I've requested that this question be deleted for the following reason:

no longer need help
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37916342
The question has been answered and further help given points should be awarded
0
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37930199
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

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now