elance
IS-IT--Management
- Sep 24, 2008
- 4
I'm not a vbscript pro, actually even sub-novice. I've tried basically piecing together snippets of code to produce the desired result. This particular code is not producing any output when it seems it should. The file gets created, and it seems as if the script is "doing something", but the file is always empty.
I am trying to search through a given set of AD groups and list users that are not a member of any of those specific groups.
Here's the code I have:
I am trying to search through a given set of AD groups and list users that are not a member of any of those specific groups.
Here's the code I have:
Code:
Option Explicit
Dim adoCommand, adoConnection, strBase, strFilter, strAttributes
Dim strQuery, adoRecordset, strName, strDN
dim objFSO
dim objStream
Set objFSO = createobject("scripting.filesystemobject")
Set objStream = objFSO.CreateTextFile("C:\test.txt", True)
' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
' Search only the specified OU.
strBase = "<LDAP://ou=desiredou,dc=domain,dc=com>"
' Filter on user objects not members of Group1 or Group2.
strFilter = "(&(objectCategory=person)(objectClass=user)" _
& "(!memberOf=cn=group1,ou=desiredou,dc=domain,dc=com)" _
& "(!memberOf=cn=group2,ou=desiredou,dc=domain,dc=com))"
' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName,distinguishedName"
' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 1000
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.
strName = adoRecordset.Fields("sAMAccountName").Value
strDN = adoRecordset.Fields("distinguishedName").value
objStream.Writeline strName & ", " & strDN
' Move to the next record in the recordset.
adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close