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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare ManagedBY for Dist List to list of usernames

Status
Not open for further replies.
Aug 15, 2006
14
US
Hello
The bottom script is used ot retrieve the ManagedBy attribute of a Distribution List as part of our termination process. Since we have thousands of DL's, when we cannot afford to have the script loop thru each DL each time a username is called.(I realize I may have cause this by the placement of some items in the script.) What I would like to do is create a dictionary array that can be used to compare the managedby attribute to the names in the list when they are called but am having some trouble

Any ideas?

' Use a dictionary object to collect all the groups
Set objList = CreateObject("Scripting.Dictionary")
objList.CompareMode = vbTextCompare

'-------------------------Reads Text File for users
Const ForReading = 1
TxtFile = "C:\scripts\Group Membership\dn.txt"
Set objFSO = CreateObject("Scripting.filesystemObject")
Set objTextFile = objFSO.OpenTextFile(TxtFile, ForReading)

'-------------------------Writing Text File

Const ForWriting = 2
Const ForAppending = 8
TxtFile1 = "C:\scripts\Group Membership\outPut.txt"
Set objFSO1 = CreateObject("Scripting.filesystemObject")
Set objTextFile1 = objFSO1.OpenTextFile(TxtFile1, ForAppending)


' Determine configuration context and DNS domain from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")


' Use ADO to search Active Directory for ObjectClass nTDSDSA.
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOOBject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

strFilter = "(&(objectCategory=Group)(objectClass=group))"
strBase = "<LDAP://Domain Info Here>"
strAttributes = "distinguishedName,DisplayName,cn,managedby"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 99999
objCommand.Properties("Timeout") = 300
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
StrDN = objRecordSet.Fields("distinguishedName")
strManager = objRecordSet.Fields("managedby")
groupCN = objRecordSet.Fields("cn")
set objGroup = GetObject("LDAP://" & strDN)
WScript.Echo groupCN & ";" & StrManager
objRecordSet.MoveNext
Loop
objRecordSet.Close


'----------UserNames to compare-------------------
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.ReadLine
sam = strNextLine


Loop
objConnection.Close

Set objConnection = Nothing
if usr = "N" Then Wscript.Echo "FindUser " & sam & " - NOT found."
Set objCommand = Nothing
Set objRootDSE = Nothing
Set objRecordSet = Nothing
 
You never appear to use the objList....add the values you wish to compare against to it....use its Exists method to help accomplish what you're looking for.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thats the problem and where I need your assistance with. I cannot figure out the placement or the proper coding for it.
 
So you want to see if the manager listed in the managedby appears in a text file you specify?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Yes please..but again without having to go thru the Dist Lists each time. I figured a Dictionary would be the best, but I am kinda stuck
 
In that case, start with opening the text file and add those user names to the dictionary...something like this

Code:
' Use a dictionary object to collect all the groups
Set objList = CreateObject("Scripting.Dictionary")
objList.CompareMode = vbTextCompare

-------------------------Reads Text File for users
Const ForReading = 1
TxtFile = "C:\scripts\Group Membership\dn.txt"
Set objFSO = CreateObject("Scripting.filesystemObject")
Set objTextFile = objFSO.OpenTextFile(TxtFile, ForReading)

Do Until objTextFile.AtEndOfStream
	strNextLine = objTextFile.ReadLine
	
	If Not objList.Exists(strNextLine) Then
		objList.Add strNextLine, ""
	End If
Loop

objTextFile.Close

In what format are you specifying the user names? Are they the same as what is visible when retrieving the value in your current code?

strManager = objRecordSet.Fields("managedby")



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
The user names are in distinguished name format, even though they appear as the display name, when you retrieve it, it prints out as the dn.
 
So do you specify the distinguished name in your text file is my real question? Would the value retrieved in your AD section match what is in the text file or do you have to convert the DN to match what you have in the text file?

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Got it...used your dictionary info to grab the user information, and then did a For each....next

---------------------------------------
Do Until objRecordSet.EOF
StrDN = objRecordSet.Fields("distinguishedName")
strManager = objRecordSet.Fields("managedby").Value
groupCN = objRecordSet.Fields("cn")

For Each dictKey in objlist.Keys
If dictkey = strManager Then
WScript.Echo dictKey & ": " & groupCN
Else
End If
Next
objRecordSet.MoveNext
Loop

------------------------------------

Works like a charm. Thanks very much for pointing me in the right direction
 
Instead of looping through the dictionary you could use

If objList.Exists(strManager) Then
...code
End If

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top