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

Enumerating Child OU's then Users in each OU problem

Status
Not open for further replies.

Vachaun22

Programmer
Oct 7, 2003
171
US
I'm having an issue trying to enumerate the OU objects in AD. I found the following code that half works, but I'm apparently referencing the wrong item in the objItem object.

Code:
Set colItems = GetObject _
    ("LDAP://ou=Servers, dc=fabrikam, dc=com")
colItems.Filter = Array("organizationalUnit")

For Each objItem in colItems
    Wscript.Echo objItem.CN
Next

I'm getting back the correct number of items for the number of OU's that are children to the OU I'm connecting to, but there is no name in the popup window. So CN must not be the correct thing to reference to get the name of the child OU. Once I get the child OU name, I can just loop through that collection changing the GetObject function parameters to get the users in each one.

I guess a recursive function would be far more useful here, but I'm having a real issue doing some of this. Ideally I'd like to simply search AD to return data in such a way to build a treeview like this:

[Domain]
|
- [Organizational Unit]
|
- [User]
- [User]
- [Organizational Unit]
|
- [Organizational Unit]
|
- [User]

and so on.

What I'm attempting to do is write a script that will read in this data from AD, then retrieve various other tidbits of information, like server shares, printers (which I already have), then be able to set parameters at user level or OU level, and place these settings in a INI file (also done), which is then read from another script as a logon script (which is done). Right now I manually created the INI file, but I would like to make it so you don't have to edit it manually, but simply create these settings in an interface. Hopefully this explains what I'm trying to do and why I'm trying to do it.

Any help would be greatly appreciated. Thanks.
 
Try...

objItem.ou
or
objItem.Name

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thank dm4ever.

One more question. Would you or anyone happen to know where to go to read information about this object to find out what other properties it contains? Besides OU, Name, etc.?
 
Ok, I've been out of programming too long...

I'd like to build this recursive function, but I'm not sure what I need to use as parameters.

I know I want to start at the root level in AD to get all the OU's.

Then I want to check each child OU for more OU's (and this step would be repeated until there are no more OU's on this branch).

Then I want to get all the Users in each OU.

After the user, move back one to the next Child OU to get it's children.

So, filtering data on User would be the terminating recursive condition.

So, that's fine that I know filtering on user is the final step, but that's all the further I can seem to get. I'm not sure what I'm passing as parameters to the recursive function.

I would think the current filter level, but for some reason, I think there needs to be more than one parameter and I'm not sure why.
 
Ok so I have the recursive function written:

Code:
Function EnumerateObjects(strFilter, strParentObject)
	On Error Resume Next
	
	strLDAP = "LDAP://" & strparentobject & "dc=domain, dc=local"

	Set colItems = GetObject _
		(strldap)

	If Err.Number <> 0 Then
		MsgBox "Error: " & Err.Number & vbCrLf & "Description: " & Err.Description, vbOKOnly, "Error"
		Err.Clear
	End If

	colItems.Filter = Array(strFilter)

	i = 0
	For Each objItem In colItems
		i = i + 1
	Next

	'If Not IsNull(colItems) Then
	If i <> 0 Then
		If strFilter = "User" Then
			For Each objItem In colItems
				MsgBox objItem.CN, vbOKOnly, "Name"
			Next
		Else
			For Each objItem In colItems
				If Len(strParentObject) = 0 Then
					strNewParentObject = "ou=" & objItem.OU & ","
				Else
					strNewParentObject = "ou=" & objItem.OU & "," & strParentObject
				End If
				
				EnumerateObjects "organizationalUnit", strNewParentObject
			Next
		End If
	Else
		If strFilter = "organizationalUnit" Then
			EnumerateObjects "User", strParentObject
		End If
	End If
End Function

But, if there are computers that exist in a OU, they are being returned when the filter is set to User. Shouldn't they only be returned if the filter is set to Computer?
 
You could also use the LDAP attribute to get the objectclass instead of using the filter.

Something like:

Code:
If objItem.objectClass = "user" Then
msgbox objItem.name
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top