Link to home
Start Free TrialLog in
Avatar of james_martin
james_martin

asked on

Filter VB.NET AD request by whenCreated

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))"
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


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
Hi james_martin,
please try this code(snippet)
thanks
 mySearcher.Filter = "(&(objectCategory=user)(whencreated>=Today.Now - 7))"

Open in new window

Avatar of james_martin
james_martin

ASKER

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.

Sorry... Needs the namespace importing:

Imports System.Text.RegularExpressions

Otherwise it won't like it :)

Chris

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

Chris

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
Chris,
Is this the correct use?
mySearcher.Filter = "(&(objectClass=User)(whenCreated<=" & dateString(-7) & "))"
sorry it reformatted it.

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

Open in new window


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
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland 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
Limited knowledge huh????  Well you rock and thanks very much!