Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Filter VB.NET AD request by whenCreated

Posted on 2009-07-06
11
Medium Priority
?
932 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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
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…
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.
Suggested Courses

715 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