Reputation: 909
My goal is to get all group settings and add the total group member count for each group along with the total owners, managers for each group.
Either array can be larger or they can be equal.
I start with getting group member count.
$GroupMembersCount = gam print groups domain <domain.com> members managers owners countsonly | ConvertFrom-Csv
Then I get the settings for each group
$GroupSettings = gam print groups settings | ConvertFrom-Csv
Both arrays share the email property and nothing else.
The $GroupMembersCount
is just a count for all the members and what right they have in each group.
looks like
email, manager, member
address.com, 1, 5
newaddress.com, 0,0
anotheraddress,3,3
etc...
the other array $GroupSettings
has like 30 column headers and one header is email addresses and the other headers are a bunch of true or false
depending on the setting for the group.
I want to combine both arrays into 1.
I know I need to do something like create a new array but the problem I have is how do I combine them since I do not know which will be larger?
$result = $null
$result = $GroupSettings + $GroupMembersCount | Select-Object -Unique
Above did not help
$Max = $null
$Max = ($GroupSettings.Count, $GroupMembersCount.count | Measure-Object -Maximum).Maximum
$resultsarray = $null
$resultsarray = For ($i = 0, $I -lt $max, $I++) {
do stuff
}
If I do something like above which array am I going to index through to get them all ?
The End result will tell me Which setting each group has, its total members, its total managers and its total owners.
I need this so I can filter groups that have no owners and managers along with other key settings
Upvotes: 1
Views: 210
Reputation: 909
Above explained the answer, here I am going to break down what I did and learned for anyone passing by with a similar problem.
What I did:
$GroupMembersCount = $null
$GroupMembersCount = gam print groups domain <domain.com> members managers owners countsonly | ConvertFrom-Csv
$GroupSettings = $null
$GroupSettings = gam print groups settings | ConvertFrom-Csv
$GroupMemberCountByEmail = @{}
$GroupMembersCount | ForEach-Object {
$GroupMemberCountByEmail[$_.email] = $_
}
$GroupSettings | Select-Object *,
@{
Name = 'MembersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].MembersCount }
},@{
Name = 'ManagersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].ManagersCount }
},@{
Name = 'OwnersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].OwnersCount }
} |
Export-Csv 'c:\temp\groups.csv' -NoTypeInformation -Encoding UTF8
The above works. The first part, what @Mathias had me build, was a hash table.
The beauty is in the $result
, here, Mathias uses calculated properties. One key point in the $result
line is as follows:
@{
Name = 'ManagersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].ManagersCount }
}
The tail of each calculated property, like .managerscount
part has to be the value in the hash created above.
There is plenty here I don't understand but, if you are looking hopefully it will lead you like it did for me.
Upvotes: 1
Reputation: 174555
Use a hashtable to "index" the first data set based on the email address:
$GroupMemberCount = gam print groups domain <domain.com> members managers owners countsonly | ConvertFrom-Csv
$GroupMemberCountByEmail = @{}
$GroupMemberCount |ForEach-Object {
$GroupMemberCountByEmail[$_.email] = $_
}
Now we can use Select-Object
to attach the relevant counts to each group setting object based on the email
property:
$result = $GroupSettings |Select *,@{Name='MemberCount';Expression={[int]$GroupMemberCountByEmail[$_.email].member}},@{Name='ManagerCount';Expression={[int]$GroupMemberCountByEmail[$_.email].manager}}
Upvotes: 2