Solved

Filter VB.NET AD request by whenCreated

Posted on 2009-07-06
11
888 Views
Last Modified: 2012-05-07
I have program that will allow some end-users to export some data from AD.  The problem I am running into is with my search code for the "whenCreate" field.  I am trying to export last 7 Days, 14 Days, and 30 Days worth of data, but just cant seem to get it to work.
mySearcher.Filter = "(&(objectClass=User)(whenCreated=Today.Now - 7))"
0
Comment
Question by:james_martin
  • 6
  • 4
11 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24785316

Hey,

The date you use must be in Universal Time format:

yyyymmddhhnnss.0Z

Perhaps like this (but there may well be much more efficient ways):

        Dim dateString As String = Date.Now.ToString("u")
        Dim RegExp As New Regex("-|:| ")
        dateString = RegExp.Replace(dateString, "")
        dateString = dateString.Replace("Z", ".0Z")
        Console.WriteLine(dateString)

You're also unlikely to get an exact match on that, many many seconds in the day and you'd have to be down to that. So this...

"(&(objectClass=User)(whenCreated>=" & dateString & "))"

Or this:

"(&(objectClass=User)(whenCreated<=" & dateString & "))"

Chris
0
 
LVL 7

Expert Comment

by:Mohed Sharfi
ID: 24785334
Hi james_martin,
please try this code(snippet)
thanks
 mySearcher.Filter = "(&(objectCategory=user)(whencreated>=Today.Now - 7))"

Open in new window

0
 

Author Comment

by:james_martin
ID: 24785389
Chris,
This declaration "Dim RegExp As New Regex("-|:| ")" is giving me an issue; "Regex" is back as not defined, how should I define it?

Thanks.
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24785404

Sorry... Needs the namespace importing:

Imports System.Text.RegularExpressions

Otherwise it won't like it :)

Chris
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24785420

Oh and I forgot the -7 days part, it'll need adding back in. Got distracted ;)

Chris
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 70

Expert Comment

by:Chris Dent
ID: 24785634

And I neglected to put in the "how to find a days worth" bit. Have the code generate something like this:

(&(objectClass=User)(whenCreated>=20090623000000.0Z)(whenCreated<=20090624000000.0Z))

Then you get those in the specified range :)

Chris
0
 

Author Comment

by:james_martin
ID: 24785725
Chris,
Is this the correct use?
mySearcher.Filter = "(&(objectClass=User)(whenCreated<=" & dateString(-7) & "))"
0
 

Author Comment

by:james_martin
ID: 24785728
sorry it reformatted it.

mySearcher.Filter = "(&(objectClass=User)(whenCreated<=" & dateString(-7) & "))"

Open in new window

0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 24785739

Not if you used mine above, dateString is a String so can't use date functions on it any more. The -7 needs to be done to the date prior to converting to a universal date string (.ToString("u")).

Chris
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 24785817

Lets see... given my limited knowledge of actual programming... I would do something like the below (just a little console app built to test this code).

Chris
Imports System.DateTime

Imports System.Text.RegularExpressions
 

Module Module1

    Function DateToUniversalDateString(ByVal dtmDate As Date)

        Dim strDate As String = dtmDate.ToString("u")

        Dim RegExp As New Regex("-|:| ")

        strDate = RegExp.Replace(strDate, "")

        strDate = strDate.Replace("Z", ".0Z")

        DateToUniversalDateString = strDate

    End Function
 

    Sub Main()

        Dim dtmStartDate As Date = Today.AddDays(-7)

        Dim dtmEndDate As Date = Today.AddDays(-6)
 

        Dim strLdapFilter As String = _

          "(&(objectClass=user)(objectCategory=person)(whenCreated>=" & _

          DateToUniversalDateString(dtmStartDate) & ")(whenCreated<=" & _

          DateToUniversalDateString(dtmEndDate) & "))"
 

        Console.WriteLine(strLdapFilter)

    End Sub
 

End Module

Open in new window

0
 

Author Closing Comment

by:james_martin
ID: 31600157
Limited knowledge huh????  Well you rock and thanks very much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Do you have users whose passwords are expiring and they are constantly calling you?  Well I sure did and needed a way to put an end to this.  We have a lot of remote users which would not be notified that their passwords were expiring since they wer…
Installing a printer using group policy preferences is not that hard let’s take a look at it. First lets open up your group policy console and edit the policy you want to add it to. I recommend creating a new policy for each printer makes it a l…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

757 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

19 Experts available now in Live!

Get 1:1 Help Now