Link to home
Start Free TrialLog in
Avatar of MilesLogan
MilesLoganFlag for United States of America

asked on

get total number of groups in AD account

Hi EE

Can someone help me modify this so it also counts the nested groups in the user object ?

GC Users.txt|GET-ADUSER –Properties name,MemberOf | Select-Object SamAccountname,@{n='GroupCount';e={ ($_.memberof).count }}
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Are you sure you want to count nested groups of users?
What are you trying to accomplish by doing so?

I hesitate to answer only because If your group structure isn't well maintained you can end up with an infinite recursion.
Avatar of MilesLogan

ASKER

I need to know the total number of groups an account is a member of which the search string above does , but I need it count nested groups also .

so if the account is a member of 10 direct groups but if five of those groups are nested in other groups , I need the total to be 15 or whatever the total may be ..
@ Miles, well, that's technically doable, but it has two technical problems with it that need to be addressed, that make it a little more difficult than a simple query.

 
Imagine this simplified scenario:

User 1 --  Member of Group_A, Group_B and Group_L
  Group_A -- No further groups it is a member of
  Group_B -- Member of Group_D
    Group_D -- Member of Group_C
      Group_C -- Member of Group B
        Group_B -- Member of Group_D
           Group_D -- Member of Group_C
             ...
You'll never even Reach this:
  Group_L -- Member of Group_A
    Group_A -- No Further Members

So you can see you can ed up with duplicates counter, and with infinite loops.

What we end up needing to do is keeping track of the actual groups we've matched so far, and skipping counting them ad expanding them when we've previously matched them.
I see and that will be the case on some of these users ..
ASKER CERTIFIED SOLUTION
Avatar of Shaun Vermaak
Shaun Vermaak
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
Thank you ! I think that will be as good as I can do .. appreciate the link