Solved

Filter VB.NET AD request by whenCreated

Posted on 2009-07-06
11
897 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 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

7 Experts available now in Live!

Get 1:1 Help Now