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

Listing and OU's and OU Members of a Domain

Status
Not open for further replies.

cmwoodman

IS-IT--Management
Sep 20, 2000
32
US
I have a script where i am able to return distinguished names of users in a domain (eg. CN=user.name,OU=NA,OU=Portal,DC=Staging,DC=com) But what i want is just 'user.name - NA - Portal'.

Does anyone know how i can parse this information? the code is pasted below. I modified it slightly from some sample scripts i have. Thanks for the help.

----Begin Code Paste----

Option Explicit

Dim objDSE, strDefaultDN, strDN, objContainer, objChild

Dim fs, res
Set fs = WScript.CreateObject("Scripting.FileSystemObject")
Set res = fs.CreateTextFile("c:\Users.txt", True)
res.Writeline ("Users details")

Set objDSE = GetObject("LDAP://rootDSE")
strDefaultDN = objDSE.Get("defaultNamingContext")

strDN = InputBox("Enter the distinguished name of a container" & _
vbCrLf & "(e.g. " & strDefaultDN & ")", , strDefaultDN)

If strDN = "" Then WScript.Quit(1) 'user clicked Cancel

Set objContainer = GetObject("LDAP://" & strDN)
Call ListUsers(objContainer)

Sub ListUsers(objADObject)
Dim objChild
For Each objChild in objADObject
Select Case objChild.Class
Case "user"
res.Writeline objChild.Name & vbTab & _
objChild.Get("distinguishedName")
Case "organizationalUnit" , "container"
Call ListUsers(objChild)
End select
Next
End Sub

---- End Code Paste ----
 
This code should help you out a little. The only problem is that if the primary group is the same as the group that you are trying to see it doesn't list that account. I'm working on that problem as we speak.

Thanks,

Ace

Option Explicit
dim GroupName, oGroup, Domain, fso, wrtlog, List2, UserCount, OU, CN
dim OutputFile, colMembers, object, user, GroupDesc, oArgs, lstrOUName

lstrOUName = GetOUName

OutputFile = "OU_GroupMembers.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
set wrtlog = fso_OpenTextFile (OutputFile, 2 , true)

'Set the following to to your domain.
Set Domain = GetObject("LDAP://server.domain.com/DC=domain,DC=Com")

'Set the following to to your domain.
set OU = GetObject("LDAP://OU=" + lstrOUName & "," + ("DC=domain,DC=Com"))

For each oGroup in oU
if OGroup.class = "group" then
GroupName = oGroup.name
GroupDesc = oGroup.Description

on error resume next
set colMembers = oGroup.Members

List2 = ""

UserCount = 0
for each user in colMembers
List2 = List2 & " " & user.name & vbcrlf
UserCount = UserCount +1
next

If UserCount = 0 then
wrtlog.WriteLine GroupName & " " & GroupDesc
wrtlog.writeline " " & GroupName & " is empty and has no members."
wrtlog.writeline
wrtlog.writeline

Else
wrtlog.writeline
wrtlog.Writeline GroupName & " " & GroupDesc
wrtlog.writeline List2
wrtlog.WriteLine " Number of Users in this group: " & UserCount
wrtlog.writeline
wrtlog.writeline
End If
Else
End if

Next
msgbox "The list is now complete!"

Function GetOUName()

Dim tempOUName

set oArgs=wscript.arguments
If oArgs.Count <> 1 Then
tempOUName = InputBox (&quot;Enter the OU Name for your Report :&quot;,&quot;Group Listing by organization Unit&quot;)
If tempOUName = &quot;&quot; Then
WScript.Quit
End If
Else
tempOUName = oArgs.item(0)
End If
GetOUName = tempOUName
End Function
 
You just have to change the part that says:

For each oGroup in oU
if OGroup.class = &quot;group&quot; then
GroupName = oGroup.name
GroupDesc = oGroup.Description

to read like below:

For each oGroup in oU
if OGroup.class = &quot;group&quot; then
GroupName = oGroup.distinguishedName
GroupDesc = oGroup.Description

Hope this helps...???
 
This Script is SWEET!, but i have several sub OU's that i am needing to list the accounts and groups listed in them as well. so my end result will be:

Main OU
-Name of OU1
-Groups
-Members of groups
-Accounts

-Name of OU2
-Groups
-Members of groups
-Accounts


I since modified the script i previously posted and it is getting close, but i am still having to deal with the distinguished name, i am looking at your script to see how i can trim all of the CN=/DN= stuff and just get something that looks like yours.

Here is my ListUsers Sub as it looks now. It is pretty rough and so are the results.

Sub ListUsers(objADObject)
Dim objChild, arrMemberOf
For Each objChild in objADObject
Select Case objChild.Class
Case &quot;user&quot;
res.Writeline objChild.Get(&quot;distinguishedName&quot;)
arrMemberOf = objChild.GetEx(&quot;memberOf&quot;)
If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
res.Writeline &quot;The memberOf attribute is not set.&quot;
Else
res.Writeline &quot;Member of: &quot;
For each Group in arrMemberOf
res.Writeline &quot;, , ,&quot; & Group
Next
End If
Case &quot;organizationalUnit&quot; , &quot;container&quot;
Call ListUsers(objChild)
End select
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top