Solved

Filter VB.NET AD request by whenCreated

Posted on 2009-07-06
11
909 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This article runs through the process of deploying a single EXE application selectively to a group of user.
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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