Dear PowerShell.org community
This is my first post, so hopefully I have not missed anything.
I have hit brick wall on the next step to troubleshoot.
In a nut shell what I am trying to achieve is to list Active Directory Group Name that starts with the prefix of AXCH (as an example) list the username\members in these Active Directory Groups and finally the count of many AD Group membership per person\user for AD groups starting with the prefix of AXCH. (AXCH is a made up example)
Some of the script is working, and getting the result I need it is just the final part, of getting the count command to work to count how many AD groups per individual user is a member of any AD group starting with the name of AXCH.
Below is the script
$adgroups = Get-ADGroup -Filter {name -like "*AXCH*"}
$data = foreach ($adgroup in $adgroups) {
$members = $adgroup | get-adgroupmember -Recursive
foreach ($member in $members) {
[PSCustomObject]@{
Group = $adgroup.name
NumberofMembersinADGroup = ($members.DistinguishedName).count
Members = $member.name
NumberofGroupsUserMemberOf = ($member.DistinguishedName).count
}
}
}
$data
So what this script s doing is from the top, find AD group that match\begin with the name of AXCH (made up name), then using the variable of $adgroups use a “for each” command to find nested ad groups and members.
The script is working up to a point and I can export to the screen or into a CSV file. The problem I am facing if the line highlighted in RED also pasted below
NumberofGroupsUserMemberOf = ($member.DistinguishedName).count
Basically I thought by using the command, would count how many AD groups per individual user is a member of any AD group starting with the name of AXCH.
Instead the command returns the count value of 1. Which I guess when I think about makes sense as it is counting the member name of the AD groups starting with AXCH and you can only have one AD account with the same name in one group. Which is perhaps I am thinking I need to do a different command, hence why I am making my first post in here.
So to clarify for example made up username called “Joe blogs” exist in several AD groups that have the prefix of AXCH (again made up AD group name) so he is a member of AXCH-1 \ AXCH-2 \ AXCH-3 & AXCH-4 what I would like is for a command to count those 4 AD groups and give me a return value of 4 against his user name, as he is a member of 4 AD groups.
At the moment my scripts returns the value of 1 even though he is a member of multiple AD groups.
If anyone could help with a command I could add to my script, or if I have to create a new script all together just so I can create a report that list the AD Group, the individual member\username, and then count value of how many AD Groups membership per member for AD groups starting with the prefix of AXCH
Many Thanks for taking the time to read this post.
Best
Paul