MSADMIN.NET

Experiences in IT
posts - 20, comments - 139, trackbacks - 173

VBScript: Finding AD Accounts Created After a Certain Date

This was a fun one. I was asked to find all user accounts in a specific OU that were created on or after August 1st 2008.

Sweet, except that I don't know how to convert the createTimeStamp attribute to a number to compare it to 08/01/2008, which I would also have to convert to a number.

You see, the createTimeStamp attribute on every user account looks something like this: 08/01/2008 8:22:48 AM .

My quick and dirty solution was simply to split the attribute into two elements of an array. See the little space between the date and the time? Bingo!

So now I have just the date. Now what? Split THAT on the slash "/" in another array, and now I have an array where the first element is the month, the second element is the day, and the third element is the year.

From there I just compared numbers. Sometimes quick and dirty works great.

Here's the script:

ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 500
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

Set objRootDSE = getObject("LDAP://rootDSE")
strDomainNCDN = objRootDSE.GET("DefaultNamingContext")
set objDomain = GetObject("LDAP://" & strDomainNCDN)

strOU = "" 'Place the OU you want to search into here, include a comma at the end, or just leave blank to search all of AD.

objCommand.CommandText = "<LDAP://" & strOU & strDomainNCDN & ">(&(objectClass=user)(objectCategory=person));name,createTimeStamp;subtree"

Set objRecordSet = objCommand.Execute

If objRecordset.RecordCount = 0 Then
WScript.Echo "Username cannot be found."
Else

While Not objRecordset.EOF

varCreateTimeStamp = objRecordset.Fields(1)
aryCreateTimeStamp = Split(varCreateTimeStamp," ")
varCreateDate = aryCreateTimeStamp(0)
aryCreateDate = Split(varCreateDate,"/")

If aryCreateDate(2) = 2008 And aryCreateDate(0) > 7 then

WScript.Echo objRecordset.Fields(0) & " Date Created: " & varCreateDate
objRecordSet.MoveNext

Else
objrecordset.MoveNext
End if

Wend
End if

Print | posted on Friday, October 17, 2008 8:27 AM

Feedback

# itsqjdna

itsqjdna
10/5/2009 1:27 PM | itsqjdna

# xbewsngt - Google Search

xbewsngt - Google Search
10/5/2009 1:27 PM |

# qloljwlv

qloljwlv
10/22/2009 2:35 AM | qloljwlv

# sauryxon - Google Search

sauryxon - Google Search
10/22/2009 2:35 AM |

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: