Hello, I am trying to enumerate all users in Active Directory and display what groups they are a member of.
My script seems to work fine when i do a wscript.echo to output the data, but when I try and write the output to a file by objfile.writeline i get an "error code: 800A01C3", "Error: Object not a collection."
please help, any advice is appreciated.
my code is
--------------------------
Option Explicit
Dim adoCommand, adoConnection,strFile, strBase, strFilter, strAttributes, WriteText
Dim objFSO, objFile, OutputFilePath, objTextFile, objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN, strMember, strGroup, arrGroups
strFile = "memberOf_out.txt"
OutputFilePath = "C:\Documents and Settings\hcabrera\My Documents\code\logs\"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFile) Then
wscript.echo "File already exists. Appending to file: " & strFile
Else
Set objFile = objFSO.CreateTextFile(strFile)
Wscript.Echo "Just created " & strFile
End If
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"
'strFilter = "(&(objectCategory=group)(|(cn=Test*)(cn=Admin*)))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,cn,MemberOf"
Set objTextFile = objFSO.OpenTextFile(strFile, 8, True)
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values and display.
arrGroups = adoRecordset.Fields("memberOf").Value
strName = adoRecordset.Fields("sAMAccountName").Value
strCN = adoRecordset.Fields("cn").value
For Each strGroup In arrGroups
'
' WriteText.writeline("Name:" & strName & vbCr)
'writeText.writeline("Memberships:" & strGroup & vbCr)
'Wscript.Echo "NT Name: " & strName & ", Common Name: " & strCN & ", Member Of: " & strGroup
objTextFile.WriteLine("NT Name: " & strName & ", Member Of: " & strGroup)
Next
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close
My script seems to work fine when i do a wscript.echo to output the data, but when I try and write the output to a file by objfile.writeline i get an "error code: 800A01C3", "Error: Object not a collection."
please help, any advice is appreciated.
my code is
--------------------------
Option Explicit
Dim adoCommand, adoConnection,strFile, strBase, strFilter, strAttributes, WriteText
Dim objFSO, objFile, OutputFilePath, objTextFile, objRootDSE, strDNSDomain, strQuery, adoRecordset, strName, strCN, strMember, strGroup, arrGroups
strFile = "memberOf_out.txt"
OutputFilePath = "C:\Documents and Settings\hcabrera\My Documents\code\logs\"
' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFile) Then
wscript.echo "File already exists. Appending to file: " & strFile
Else
Set objFile = objFSO.CreateTextFile(strFile)
Wscript.Echo "Just created " & strFile
End If
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
Set adoCommand.ActiveConnection = adoConnection
' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"
' Filter on user objects.
strFilter = "(&(objectCategory=person)(objectClass=user))"
'strFilter = "(&(objectCategory=group)(|(cn=Test*)(cn=Admin*)))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,cn,MemberOf"
Set objTextFile = objFSO.OpenTextFile(strFile, 8, True)
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False
' Run the query.
Set adoRecordset = adoCommand.Execute
' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
' Retrieve values and display.
arrGroups = adoRecordset.Fields("memberOf").Value
strName = adoRecordset.Fields("sAMAccountName").Value
strCN = adoRecordset.Fields("cn").value
For Each strGroup In arrGroups
'
' WriteText.writeline("Name:" & strName & vbCr)
'writeText.writeline("Memberships:" & strGroup & vbCr)
'Wscript.Echo "NT Name: " & strName & ", Common Name: " & strCN & ", Member Of: " & strGroup
objTextFile.WriteLine("NT Name: " & strName & ", Member Of: " & strGroup)
Next
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close