B
Ben Gagnon
How does one return the USN-Created property of a user object?
I'm trying to write a script that simply grabs the sAMAccountName and the
USN-Created attributes of an OU full of user objects. When I attempt to
read the uSNCreated record from the recordset and append it to a string, I
get a type mismatch whatever I do. The MSDN library lists the USN-Created
attribute as a simple long integer. I don't believe them anymore. The
USN-Created attribute is defined in the Top class.
Here's my code: Any help is appreciated.
' Quick script outputs a text file containing user login name and USNCreated
field on one line
' per user, comma-delimited. Returns all users in UNames OU in Classrooms
domain.
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8
Const OU = "OU=UNames,"
Const OUTPUT_FILE = "USNData.txt"
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")
' Open the output file for appending
Set objFS = CreateObject("Scripting.FileSystemObject")
Set tsOutFile = objFS.OpenTextFile(OUTPUT_FILE, FOR_APPENDING, True)
'Set up the ODB database connection to Active Directory
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "AdsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
' Build the query string
strSQL = "SELECT sAMAccountName, uSNCreated " _
& "FROM 'LDAP:// " & OU & strDomain& "' "_
& "WHERE objectClass='user' " _
' Execute the query
objCommand.Properties("Page Size") = 50
objCommand.Properties("Asynchronous") = True
objCommand.CommandText = strSQL
Set rsRecordSet = objCommand.Execute
rsRecordSet.MoveFirst
If rsRecordSet.EOF Then
MsgBox "Empty recordset."
WScript.Quit
End If
Do While Not rsRecordSet.EOF
strLineOut = rsRecordSet.Fields("sAMAccountName") & ","
If Not IsNull(rsRecordSet.Fields("uSNCreated") ) Then
strLineOut = strLineOut & rsRecordSet.Fields("uSNCreated")
End If
tsOutFile.WriteLine( cStr(strLineOut) )
rsRecordSet.MoveNext
Loop
' Clean Up
tsOutFile.Close
Set tsOutFile = Nothing
Set objRootDSE = Nothing
Set objFS = Nothing
Set rsRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
--
---------------------------------------
Ben Gagnon
Electronics Technician
PC Classroom Operations/OIT
bgagnon(at)oit(dot)umass(dot)edu
University of Massachusetts, Amherst
I'm trying to write a script that simply grabs the sAMAccountName and the
USN-Created attributes of an OU full of user objects. When I attempt to
read the uSNCreated record from the recordset and append it to a string, I
get a type mismatch whatever I do. The MSDN library lists the USN-Created
attribute as a simple long integer. I don't believe them anymore. The
USN-Created attribute is defined in the Top class.
Here's my code: Any help is appreciated.
' Quick script outputs a text file containing user login name and USNCreated
field on one line
' per user, comma-delimited. Returns all users in UNames OU in Classrooms
domain.
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8
Const OU = "OU=UNames,"
Const OUTPUT_FILE = "USNData.txt"
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")
' Open the output file for appending
Set objFS = CreateObject("Scripting.FileSystemObject")
Set tsOutFile = objFS.OpenTextFile(OUTPUT_FILE, FOR_APPENDING, True)
'Set up the ODB database connection to Active Directory
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "AdsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
' Build the query string
strSQL = "SELECT sAMAccountName, uSNCreated " _
& "FROM 'LDAP:// " & OU & strDomain& "' "_
& "WHERE objectClass='user' " _
' Execute the query
objCommand.Properties("Page Size") = 50
objCommand.Properties("Asynchronous") = True
objCommand.CommandText = strSQL
Set rsRecordSet = objCommand.Execute
rsRecordSet.MoveFirst
If rsRecordSet.EOF Then
MsgBox "Empty recordset."
WScript.Quit
End If
Do While Not rsRecordSet.EOF
strLineOut = rsRecordSet.Fields("sAMAccountName") & ","
If Not IsNull(rsRecordSet.Fields("uSNCreated") ) Then
strLineOut = strLineOut & rsRecordSet.Fields("uSNCreated")
End If
tsOutFile.WriteLine( cStr(strLineOut) )
rsRecordSet.MoveNext
Loop
' Clean Up
tsOutFile.Close
Set tsOutFile = Nothing
Set objRootDSE = Nothing
Set objFS = Nothing
Set rsRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
--
---------------------------------------
Ben Gagnon
Electronics Technician
PC Classroom Operations/OIT
bgagnon(at)oit(dot)umass(dot)edu
University of Massachusetts, Amherst