JohnCee53 said:
How would I get this to work using the last 7 days instead of just today?
The
reason I ask is that my manager has asked me to come up with a way to run
a
report on Friday mornings that would show users a) disabled; b) created,
and
c) enabled during the last 7 days. I've already tested the above code and
tailored it for my use. If nothing else I will manually merge the last 7
days
reports in to at least two reports for created and disabled users for the
last 7 days.
I wouldn't know how to modify the command line for CSVDE to do what you
want, but you can get similar results from a VBScript program. The example
above spits out values of employeeID, sn, givenName, and sAMAccountName for
all user objects where the whenCreated attribute is between the beginning of
the day 7 days ago and the end of today:
=============
Option Explicit
Dim objRootDSE, strDNSDomain, adoConnection
Dim strBase, strFilter, strAttributes, strQuery, adoRecordset
Dim dtmStart, dtmEnd, strStart, strEnd
Dim strID, strFirst, strLast, strNTName
dtmEnd = Now()
dtmStart = DateAdd("d", -7, dtmEnd)
strStart = CStr(Year(dtmStart)) _
& Right("0" & CStr(Month(dtmStart)), 2) _
& Right("0" & CStr(Day(dtmStart)), 2) & "000000.0Z"
strEnd = CStr(Year(dtmEnd)) _
& Right("0" & CStr(Month(dtmEnd)), 2) _
& Right("0" & CStr(Day(dtmEnd)), 2) & "235959.0Z"
' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use ADO to search Active Directory.
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoRecordset = CreateObject("ADODB.Recordset")
adoRecordset.ActiveConnection = adoConnection
' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(whenCreated>=" & strStart & ")(whenCreated<=" & strEnd & "))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "employeeID,sn,givenName,sAMAccountName"
' Construct the LDAP query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
' Run the query.
adoRecordset.Source = strQuery
adoRecordset.Open
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values.
strID = adoRecordset.Fields("employeeID").Value
strLast = adoRecordset.Fields("sn").Value
strFirst = adoRecordset.Fields("givenName").Value
strNTName = adoRecordset.Fields("sAMAccountName").Value
Wscript.Echo """" & strID & """,""" & strLast & """,""" & strFirst
& """,""" & strNTName & """"
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
==========
The above should be run at a command prompt using the cscript host so the
output can be redirected to a text file. For example, if the VBScript
program is saved in the file FindUsers.vbs:
cscript //nologo FindUsers.vbs > users.csv
This assumes you are in the directory where the file FindUsers.vbs is saved.
Otherwise, you must specify the full path. Note this will not reveal cases
where users are disabled or enabled in the last 7 days, only created.