We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Filter VB.NET AD request by whenCreated

Medium Priority
979 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))"
Comment
Watch Question

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

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
Mohed SharfiVice CEO

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

Open in new window

Author

Commented:
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.
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

Sorry... Needs the namespace importing:

Imports System.Text.RegularExpressions

Otherwise it won't like it :)

Chris
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

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

Chris
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

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

Author

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

Author

Commented:
sorry it reformatted it.

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

Open in new window

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:

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
PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Limited knowledge huh????  Well you rock and thanks very much!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.