Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to populate a dictionary object from a recordset?

Status
Not open for further replies.

Exidy

Technical User
Feb 26, 2005
12
US
Hopefully you guys can help with this.

Here's a chunk of code that looks at every user in an OU and plucks out those users' homeDirectory and userPrincipalName attributes:

Code:
strBase = "<LDAP://ou=Users,ou=Site1,ou=Sites,dc=mycompany,dc=com>"
strFilter = "(&(objectCategory=person)(objectClass=user))"
strAttributes = "homeDirectory,userPrincipalName"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery

Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst 
Do Until objRecordSet.EOF 
  strHomeDirectory = objRecordSet.Fields("homeDirectory").Value
  strUsername = objRecordSet.Fields("userPrincipalName").Value
  For Each strHomeDirectory In objRecordSet.Fields
      If InStr(strHomeDirectory, "server01") > 0 then   
          Group1.Add strUsername, "server01"
      Else 
          If InStr(strHomeDirectory, "server02") > 0 Then 
              Group2.Add strUsername, "server02"
          End If  
       End If 
  Next  
  objRecordSet.MoveNext 
Loop

When I try to run it, though, the error returned is "This key is already associated with an element of this collection."

Basically I need a way to take all the homeDirectory and userPrincipalName attributes that the loop collects for all the users and stick them into two separate dictionary objects. So, at the end, I'd have two dictionary objects, one filled with a list of all users on server01, and one filled with a list of all users on server02.

What would be the best way to go about doing this?
 
In early versions of my script, I was using SQL, but I ran into trouble when trying to collect all users (this was before I realized that there were exclusions I needed to account for) and so I switched over to LDAP.

I'm even more of a novice with SQL - could you perhaps give me a general example of how to exclude an AD attribute in a SQL query?
 
sorry i am no expert on SQL query syntax either and dont have my book with me at work...but

shoudl be something lke

SELECT a,b,c FROM ... WHERE A IS LIKE = 'ext*' AND NOT...

i presume it is the use of NOT or <> that is the key to the exclusion using SQL syntax?
 
SELECT distinguishedname,samaccountname,extensionattribute3,extensionattribute4 FROM 'LDAP://servername' WHERE extensionattribute4 LIKE 'AB*' AND extensionattribute3 LIKE 'CD*' ORDERBY extensionattribute4 asc"

i guess you might want, (see the NOT added)

"SELECT distinguishedname,samaccountname,extensionattribute3,extensionattribute4 FROM 'LDAP://servername' WHERE extensionattribute4 LIKE 'AB*' AND extensionattribute3 NOT LIKE 'CD*' ORDERBY extensionattribute4 asc"


 
Thanks. I will play around with it and see what happens...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top