Solved

Filter VB.NET AD request by whenCreated

Posted on 2009-07-06
11
923 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 71

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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 
LVL 71

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 71

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
 
LVL 71

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 71

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 71

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

636 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