Link to home
Start Free TrialLog in
Avatar of nightshadz
nightshadzFlag for United States of America

asked on

Visual Basic .NET & Active Directory filter/search

Below is an AD Schema I have set up in a test environment.  My requirement is to loop through all of the members in the "RSA Users" and set a complex password.  

I have most of the code set up already but I'm not sure how to access the "RSA Users" group, loop through all the members in there, and set their password.  

 User generated image
Imports System
Imports System.DirectoryServices

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)

        dirSearcher.Filter = ""
        dirSearcher.SearchScope = SearchScope.Subtree
        Dim searchResults As SearchResult = dirSearcher.FindOne()

        If Not searchResults Is Nothing Then
            Dim dirEntryResults As New DirectoryEntry(searchResults.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, "lwsimsid", "1156")
            '   3. Commit the changes
            dirEntryResults.CommitChanges()
            '   4. Close & Cleanup
            dirEntryResults.Close()
        End If
        '   4a. Close & Cleanup
        dirEntry.Close()

    End Sub

    Public Shared Function GetDirectoryEntry() As DirectoryEntry
        Dim dirEntry As DirectoryEntry = New DirectoryEntry()
        dirEntry.Path = "LDAP://OU=APPLICATION GROUPS,DC=Test,DC=local"
        Return dirEntry
    End Function

    Public Shared Sub SetProperty(ByVal de As DirectoryEntry, ByVal PropertyName As String, ByVal PropertyValue As String)
        If Not PropertyValue Is Nothing Then
            If de.Properties.Contains(PropertyName) Then
                de.Properties(PropertyName)(0) = PropertyValue
            Else
                de.Properties(PropertyName).Add(PropertyValue)
            End If
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim i As Integer

        For i = 1 To 20
            Debug.Print(RandomPassword.Generate(10))
        Next
    End Sub
End Class

Open in new window

Avatar of nightshadz
nightshadz
Flag of United States of America image

ASKER

I've gotten this far, but I can't figure out how to set properties for "CurrentMember".
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
        Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)

        dirSearcher.SearchRoot = dirEntry
        dirSearcher.Filter = "(&(ObjectClass=Group)(CN=RSA Users))"

        Dim Members As Object = dirSearcher.FindOne.GetDirectoryEntry.Invoke("Members", Nothing)
        For Each Member As Object In CType(Members, IEnumerable)
            Dim CurrentMember As New DirectoryEntry(Member)
            Debug.Print(CurrentMember.Name)
            SetProperty(CurrentMember, "lwsimsid", "1245")
            'SetProperty(CurrentMember, "Password", "abc")
        Next
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nightshadz
nightshadz
Flag of United States of America 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