Lider13
Lider13

Reputation: 47

List of Active Directory users created on a specific date

Public Class Filters
    Inherits AdvancedFilters
    Public Sub New(ByVal p As Principal)
        MyBase.New(p)
    End Sub
    Public Sub CreatedAfter(ByVal value As Date)
        Dim strValue = value.ToString("yyyyMMddHHmmss.0Z")
        AdvancedFilterSet("WhenCreated", strValue, GetType(String), MatchType.GreaterThanOrEquals)
    End Sub
End Class


 Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Using context = New PrincipalContext(ContextType.Domain)
            Using userPrin As New UserPrincipal(context) With {
                .Enabled = True}
                Dim filter = New Filters(userPrin)
                filter.CreatedAfter(Date.UtcNow.AddDays(-20))
                For Each result In (New PrincipalSearcher(userPrin)).FindAll()
                    Dim de As DirectoryEntry = TryCast(result.GetUnderlyingObject(), DirectoryEntry)
                    Dim dn1 As String = de.Properties("CN")(0)
                    Dim dn2 As String = de.Properties("whenCreated")(0)
                    DataGridView1.Rows.Add(dn1, dn2)
                Next result
            End Using
        End Using
    End Sub

The code displays created users for the last 20 days from the current date.

How you can I display a list of users created by the date selected from DateTimePicker +20 days.

For example:

DateTimePicker = 09/08/2021

The list of users should include users with the creation date from 08/09/2021+20days (28/09/2021)

Upvotes: 0

Views: 532

Answers (1)

Lider13
Lider13

Reputation: 47

Found a solution. If someone tells me how to do it better, I will be grateful.

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
       DataGridView1.Rows.Clear()
       Using context = New PrincipalContext(ContextType.Domain)
           Using userPrin As New UserPrincipal(context) With {
               .Enabled = True}
               Dim filter = New Filters(userPrin)
              
               Dim FirstDate As Date = DateTimePicker1.Value
               Dim SecondDate As Date = DateTimePicker2.Value
               Dim ts As TimeSpan = SecondDate.Subtract(FirstDate)
               Dim differenceInDays As Integer = ts.Days

               filter.CreatedAfter(SecondDate.AddDays(-differenceInDays - 1))
               For Each result In (New PrincipalSearcher(userPrin)).FindAll()
                   Dim de As DirectoryEntry = TryCast(result.GetUnderlyingObject(), DirectoryEntry)
                   Dim dn1 As String = de.Properties("CN")(0)
                   Dim dn2 As String = de.Properties("whenCreated")(0)
                   DataGridView1.Rows.Add(dn1, dn2)
               Next result
           End Using
       End Using
   End Sub

Upvotes: 0

Related Questions