Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Can't figure out what to put for argument

form

  • Please log in to reply
2 replies to this topic

#1 unclepickle1

unclepickle1

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 08 March 2012 - 10:32 AM

Hey guys,

I'm using a snippet of code that can be found here

http://www.codeproje...ctory-in-VB-NET

It is a sub that is meant to add a user to a group in Active Directory. However, I can't seem to figure out how to use it. Here is the code:

''' <summary>
        ''' Method to add a user to a group
        ''' </summary>
        ''' <param name="de">DirectoryEntry to use</param>
        ''' <param name="deUser">DirectoryEntry (User) to use</param>
        ''' <param name="GroupName">Group name (in string formation) to search and ultimately add user to</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 New DirectoryEntry(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

I'm not sure what to put for the first argument when calling it. For example:

AddUserToGroup(?, User, Group)

Thanks
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 08 March 2012 - 10:43 AM

        ''' <param name="de">DirectoryEntry to use</param>


The purpose of that variable is stated in the documentation above the sub.

DirectoryEntry doc:
DirectoryEntry Class (System.DirectoryServices)

As the doc page says,

The DirectoryEntry class encapsulates a node or object in the Active Directory Domain Services hierarchy.


This would be whatever Organizational Unit you've created that you want to add your user under.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 unclepickle1

unclepickle1

    CC Newcomer

  • New Member
  • PipPip
  • 10 posts

Posted 12 March 2012 - 07:52 AM

Great, thanks!
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download