• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 567
  • Last Modified:

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.  

 AD Schema
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

0
nightshadz
Asked:
nightshadz
  • 2
1 Solution
 
nightshadzAuthor Commented:
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

0
 
nightshadzAuthor Commented:
Never mind I figured it out.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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