Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Get a tree view of groups and users inside a group.

Hi,

I have a group which has 100's of users and 100's of groups in it.

Any way that i get a tree view of the groups
Ex:

Main Group
Member1
member2
Group1
Member1
Member2
Group2
Member1

So on...


Regards
Sharath
Avatar of ahoffmann
ahoffmann
Flag of Germany image

could you please post an example text how "have a group" looks like
Avatar of RobSampson
Sharath, I don't understand what you need for this.....

Can you provide an example of what's in each of your files, and the output you would like?

Regards,

Rob.
Avatar of bsharath

ASKER

Rob

I have a Group called "Chennai"

What i need is a script to query the group"Chennai" and get all the users and groups that are a member of the group "Chennai"

In a tree way.

Like "Sharath is a member of "Chennai" but indirectly .Say i am a member in a group called "India"
and "India" is a member of "Chennai."
so i need like

Sharath
> India
>> Chennai

Only for the users who are in the group "Chennai"

You haad already given me a solution for all users in the Domain or all users in a file.
The same way i need only for all users in the "Group"
Users how they are a member in this group.In a tree structure.
If you use this script:

'==========
Set objNetwork = CreateObject("WScript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
'MsgBox objSysInfo.UserName
'Set objUser = GetObject("LDAP://" & "CN=TestComputers,OU=Test Computers,OU=Computers,OU=Civic Centre,OU=Sites,DC=maroondah,DC=local")
Set objUser = GetObject("LDAP://" & "CN=TestGroup2,OU=Users,OU=TestOU,DC=maroondah,DC=local")

strGroups = ""

intLevel = 0

GetMemberOfNames objUser, intLevel

strResults = Replace(objUser.Name, "CN=", "") & " is a member of: "
arrGroups = Split(strGroups, VbCrLf)
For intCount = LBound(arrGroups) To UBound(arrGroups)
      If strResults = "" Then
            strResults = arrGroups(intCount)
      Else
            strResults = strResults & VbCrLf & arrGroups(intCount)
      End If
Next

MsgBox strResults

Sub GetMemberOfNames(objObjectToCheck, intLevel)
      ' This function can get caught in a loop if there is a circular
      ' group membership.  There is a method of using a Dictionary object
      ' here: http://www.rlmueller.net/MemberOf.htm
      ' which checks if the group has been used before.
      
      intLevel = intLevel + 1
      ' Retrieve ALL of the user groups that a user is a member of
      On Error Resume Next
      objMemberOf = objObjectToCheck.GetEx("MemberOf")
      If Err.Number = 0 Then
            On Error GoTo 0
            For Each objGroup in objMemberOf
                  strGroupName = Left(Mid(objGroup, InStr(objGroup, "CN=") + 3),InStr(Mid(objGroup, InStr(objGroup, "CN=") + 3), ",") - 1)
                  If strGroups = "" Then
                        strGroups = String(intLevel, ">") & strGroupName
                  Else
                        strGroups = strGroups & VbCrLf & String(intLevel, ">") & strGroupName
                  End If
                  Set objNextGroup = GetObject("LDAP://" & objGroup)
                  GetMemberOfNames objNextGroup, intLevel
            Next
            intLevel = intLevel - 1
      Else
            intLevel = intLevel - 1
            Err.Clear
            On Error GoTo 0
      End If
End Sub
'==========

And then change
Set objUser = GetObject("LDAP://" & "CN=TestGroup2,OU=Users,OU=TestOU,DC=maroondah,DC=local")

to the full path to the group....it should work.

Regards,

Rob.
Rib i get this...

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

---------------------------
Chennai is a member of:

>CHENNAI_India

>>INDIA

>>>PRODUCT_GROUP
---------------------------
OK  
---------------------------
But there are many members in it.I need to get a report of all users in it.But i get only for the group.

There is sharth as a member so i need to get as

Sharath is a member of :
>Chennai
>India

But i get only for that 1 group
So by specifying a group, you want to get all members in that group, AND all groups that the *group* is a member of?  I'm sorry, I'm confused which direction you want this in.

At the moment, you specify a group, and it goes "upwards", though the Member Of rab, not the Members tab.....

Regards,

Rob.
Rob it should take user inside the group "Chennai" and get the group he is.

I just want to know in which group he/she is a member and added to the group "Chennai"

There are 100's of groups inside 'Chennai" as members many users inside the group "Chennai' are part of other groups and they are added to this group.So need to know in which group they are a member and have een into the "Chennai" group.

So, within the Chennai group, you want to go through its members, and then for users only, not groups, you want to output each users's MemberOf tab.....is that right?

Regards,

Rob.
Yes Rob...

When it goes to users it has to get all nested users details also.
Like 1 user can be a member of in many group.So i want to know that.From which group has this user been added.
See if this gives you what you need:

'==========
strOutputFile = "GroupMembership.txt"

Set objNetwork = CreateObject("WScript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
'MsgBox objSysInfo.UserName
'Set objUser = GetObject("LDAP://" & "CN=TestComputers,OU=Test Computers,OU=Computers,OU=Civic Centre,OU=Sites,DC=maroondah,DC=local")
Set objGroup = GetObject("LDAP://" & "CN=TestGroup2,OU=Users,OU=TestOU,DC=maroondah,DC=local")

strGroups = "Members of " & Replace(objGroup.Name, "CN=", "") & " are in the following groups:"

intLevel = 0

For Each objUser In objGroup.Members
      strGroups = strGroups & VbCrLf & VbCrLf & Replace(objGroup.Name, "CN=", "") & " Member: " & Replace(objUser.Name, "CN=", "")
      GetMemberOfNames objUser, intLevel
Next

'strResults = "Members of " & objGroup.Name
arrGroups = Split(strGroups, VbCrLf)
For intCount = LBound(arrGroups) To UBound(arrGroups)
      If strResults = "" Then
            strResults = arrGroups(intCount)
      Else
            strResults = strResults & VbCrLf & arrGroups(intCount)
      End If
Next

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.Write strResults
objOutputFile.Close
Set objOutputFile = Nothing

MsgBox "Done. Please see " & strOutputFile

Sub GetMemberOfNames(objObjectToCheck, intLevel)
      ' This function can get caught in a loop if there is a circular
      ' group membership.  There is a method of using a Dictionary object
      ' here: http://www.rlmueller.net/MemberOf.htm
      ' which checks if the group has been used before.
     
      intLevel = intLevel + 1
      ' Retrieve ALL of the user groups that a user is a member of
      On Error Resume Next
      objMemberOf = objObjectToCheck.GetEx("MemberOf")
      If Err.Number = 0 Then
            On Error GoTo 0
            For Each objGroup in objMemberOf
                  strGroupName = Left(Mid(objGroup, InStr(objGroup, "CN=") + 3),InStr(Mid(objGroup, InStr(objGroup, "CN=") + 3), ",") - 1)
                  If strGroups = "" Then
                        strGroups = String(intLevel, ">") & strGroupName
                  Else
                        strGroups = strGroups & VbCrLf & String(intLevel, ">") & strGroupName
                  End If
                  Set objNextGroup = GetObject("LDAP://" & objGroup)
                  GetMemberOfNames objNextGroup, intLevel
            Next
            intLevel = intLevel - 1
      Else
            intLevel = intLevel - 1
            Err.Clear
            On Error GoTo 0
      End If
End Sub
'==========

Regards,

Rob.
I get this..


C:\>Get_Tree_View_Of_Group_Members.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\Get_Tree_View_Of_Group_Members.vbs(15, 7) Microsoft VBScript runtime error: O
bject required: 'objGroup'


C:\>
Did you change the path of objGroup on this line:
Set objGroup = GetObject("LDAP://" & "CN=TestGroup2,OU=Users,OU=TestOU,DC=maroondah,DC=local")

Regards,

Rob.
Yes Rob i have changed it
That's really odd.  I don't get that error at all.  That's basically saying that objGroup could not be created by the line
Set objGroup = GetObject("LDAP://" & "CN=TestGroup2,OU=Users,OU=TestOU,DC=maroondah,DC=local")

Directly above this line:
strGroups = "Members of " & Replace(objGroup.Name, "CN=", "") & " are in the following groups:"

if you put
MsgBox objGroup.Name

it should do exactly the same thing that line 15 was trying to do.

Regards,

Rob.
I get a box like this.
---------------------------

---------------------------
CN=Chennai
---------------------------
OK  
---------------------------

Then the same error message.
As i have many groups as the same with a little change
The group i want to query is chennai
i even have chennaiia and many more as these.
so yu simply want to in which groups a specific user is?
If so, that's a simple ldapsearch oneliner ...
Are you using a group that is in the same domain as you? Can you try that?

Regards,

Rob.
Rob even for a local group i get the same error.
Can you confirm that this line is giving you the error?
strGroups = "Members of " & Replace(objGroup.Name, "CN=", "") & " are in the following groups:"

As I said, that's so odd, because you have this above that:
MsgBox objGroup.Name

and it's exactly the same thing????  Can you try to copy and paste the whole code again?

Regards,

Rob.
Here is the whole code...

'==========
strOutputFile = "GroupMembership.txt"

Set objNetwork = CreateObject("WScript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
'MsgBox objSysInfo.UserName
'Set objUser = GetObject("LDAP://" & "CN=TestComputers,OU=Test Computers,OU=Computers,OU=Civic Centre,OU=Sites,DC=maroondah,DC=local")
Set objGroup = GetObject("LDAP://" & "CN=Chennai_fs-sg,OU=Security Groups,OU=Countries,DC=Development,DC=co,DC=uk")
MsgBox objGroup.Name
strGroups = "Members of " & Replace(objGroup.Name, "CN=", "") & " are in the following groups:"

intLevel = 0

For Each objUser In objGroup.Members
      strGroups = strGroups & VbCrLf & VbCrLf & Replace(objGroup.Name, "CN=", "") & " Member: " & Replace(objUser.Name, "CN=", "")
      GetMemberOfNames objUser, intLevel
Next

'strResults = "Members of " & objGroup.Name
arrGroups = Split(strGroups, VbCrLf)
For intCount = LBound(arrGroups) To UBound(arrGroups)
      If strResults = "" Then
            strResults = arrGroups(intCount)
      Else
            strResults = strResults & VbCrLf & arrGroups(intCount)
      End If
Next

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.Write strResults
objOutputFile.Close
Set objOutputFile = Nothing

MsgBox "Done. Please see " & strOutputFile

Sub GetMemberOfNames(objObjectToCheck, intLevel)
      ' This function can get caught in a loop if there is a circular
      ' group membership.  There is a method of using a Dictionary object
      ' here: http://www.rlmueller.net/MemberOf.htm
      ' which checks if the group has been used before.
     
      intLevel = intLevel + 1
      ' Retrieve ALL of the user groups that a user is a member of
      On Error Resume Next
      objMemberOf = objObjectToCheck.GetEx("MemberOf")
      If Err.Number = 0 Then
            On Error GoTo 0
            For Each objGroup in objMemberOf
                  strGroupName = Left(Mid(objGroup, InStr(objGroup, "CN=") + 3),InStr(Mid(objGroup, InStr(objGroup, "CN=") + 3), ",") - 1)
                  If strGroups = "" Then
                        strGroups = String(intLevel, ">") & strGroupName
                  Else
                        strGroups = strGroups & VbCrLf & String(intLevel, ">") & strGroupName
                  End If
                  Set objNextGroup = GetObject("LDAP://" & objGroup)
                  GetMemberOfNames objNextGroup, intLevel
            Next
            intLevel = intLevel - 1
      Else
            intLevel = intLevel - 1
            Err.Clear
            On Error GoTo 0
      End If
End Sub
'==========
Rob i dont know if i am doing some thing wrong...
Rob any help on the posts :-)
Hi Sharath, I know, I've still got about six of yours to follow up....I'm really busy at the moment, sorting out my wedding in a couple of weeks, and getting work ready to cope without me for four weeks..... I'll try to get to them later this week.

Regards,

Rob.
Great news Rob.
                    Happy for you...
                                            Dont worry about the Q...'s.
                                                                                         Have fun...
Thanks Sharath, I'll do what I can before I go.....this is the code that works for me....by the way, I like this new "code snippet" feature....

and I can't figure out why you'd by getting object required.....the object should be created!

Regards,

Rob.
'==========
strOutputFile = "GroupMembership.txt"
 
Set objNetwork = CreateObject("WScript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
'Set objGroup = GetObject("LDAP://" & "CN=TestComputers,OU=Test Computers,OU=Computers,OU=Civic Centre,OU=Sites,DC=maroondah,DC=local")
Set objGroup = GetObject("LDAP://" & "CN=Chennai_fs-sg,OU=Security Groups,OU=Countries,DC=Development,DC=co,DC=uk")
MsgBox objGroup.Name
strGroups = "Members of " & Replace(objGroup.Name, "CN=", "") & " are in the following groups:"
 
intLevel = 0
 
For Each objUser In objGroup.Members
      strGroups = strGroups & VbCrLf & VbCrLf & Replace(objGroup.Name, "CN=", "") & " Member: " & Replace(objUser.Name, "CN=", "")
      GetMemberOfNames objUser, intLevel
Next
 
'strResults = "Members of " & objGroup.Name
arrGroups = Split(strGroups, VbCrLf)
For intCount = LBound(arrGroups) To UBound(arrGroups)
      If strResults = "" Then
            strResults = arrGroups(intCount)
      Else
            strResults = strResults & VbCrLf & arrGroups(intCount)
      End If
Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.Write strResults
objOutputFile.Close
Set objOutputFile = Nothing
 
MsgBox "Done. Please see " & strOutputFile
 
Sub GetMemberOfNames(objObjectToCheck, intLevel)
      ' This function can get caught in a loop if there is a circular
      ' group membership.  There is a method of using a Dictionary object
      ' here: http://www.rlmueller.net/MemberOf.htm
      ' which checks if the group has been used before.
      
      intLevel = intLevel + 1
      ' Retrieve ALL of the user groups that a user is a member of
      On Error Resume Next
      objMemberOf = objObjectToCheck.GetEx("MemberOf")
      If Err.Number = 0 Then
            On Error GoTo 0
            For Each objGroup in objMemberOf
                  strGroupName = Left(Mid(objGroup, InStr(objGroup, "CN=") + 3),InStr(Mid(objGroup, InStr(objGroup, "CN=") + 3), ",") - 1)
                  If strGroups = "" Then
                        strGroups = String(intLevel, ">") & strGroupName
                  Else
                        strGroups = strGroups & VbCrLf & String(intLevel, ">") & strGroupName
                  End If
                  Set objNextGroup = GetObject("LDAP://" & objGroup)
                  GetMemberOfNames objNextGroup, intLevel
            Next
            intLevel = intLevel - 1
      Else
            intLevel = intLevel - 1
            Err.Clear
            On Error GoTo 0
      End If
End Sub
'==========

Open in new window

Rob i think we have problems with this new feature.When i copy and paste in txt file then it comes in an unformatted way...
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you post the code as usual...
I get this...
C:\>Get_Tree_View_Of_Group_Members.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\Get_Tree_View_Of_Group_Members.vbs(14, 7) Microsoft VBScript runtime error: O
bject required: 'objGroup'
That just doesn't make sense....the only thing I can think of is if the Name property is not set for the group (but that's not really possible).....try this, instead of this line:

strGroups = strGroups & VbCrLf & VbCrLf & Replace(objGroup.Name, "CN=", "") & " Member: " & Replace(objUser.Name, "CN=", "")

put these:

      On Error Resume Next
      strText = Replace(objGroup.Name, "CN=", "") & " Member: " & Replace(objUser.Name, "CN=", "")
      If Err.Number = 0 Then
            On Error GoTo 0
            strGroups = strGroups & VbCrLf & VbCrLf & strText
      Else
            Err.Clear
            On Error GoTo 0
            strGroups = strGroups & VbCrLf & VbCrLf & "Member: " & Replace(objUser.Name, "CN=", "")
      End If


Regards,

Rob.
Thanks a lot Rob...Excellent help...
Thanks Rob...

The EE is changing a lot...

They have introduced the Accept feature in a very nice way....

I am sure you will not see it as you dont ask any you dont accept a question to see this new feature. :)
Ha ha, yeah that's true....I don't get see the "asker" side of things....unless I'm really stuck!

So is the output OK from this text file.....that was a very strange problem....

Rob.
Yes Rob that's what i wanted...
Rob i just tried with the Domain users group.Which is the group which has all users in it.So it would be easy to find if any user is missing in group called "Chennai"

I get the box and no errors in the txt file i just get this...
Members of Domain users are in the following groups:

Will it not work for Domain users
Are you specifying the Domain Users groups as in:
"LDAP://" & "CN=Domain Users,CN=Users,DC=development,DC=co,DC=uk"

The Domain Users groups is actually in a Users container, not an OU....

Regards,

Rob.
Thanks Rob just tryed its working after changing the container