Reputation: 909
today I asked combined two arrays of different size with different properties
Here is the answer
$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
How can I add a column to this answer above that will give me a True or false if all counts are zero ?
I think I need if
before the export above but I am not sure how to If (X -eq 0 -and Y -eq 0) { Do stuff }
here since I don't know how to address the X before the export.
| Export-Csv 'c:\temp\groups.csv' -NoTypeInformation -Encoding UTF8
Upvotes: 1
Views: 69
Reputation: 909
Again I want to add what I did. Please note to any reader some of my answer is different because I use a tool called GAM to help me administrate Google. GAM forced me to pivot from what I accepted above.
PS: I find with most answers here there are adjustments needed. Your mileage may vary so tread carefully.
<#
https://stackoverflow.com/questions/70613438/how-could-i-add-an-if-statement-when-combining-two-different-arrays/70614250#70614250
https://stackoverflow.com/questions/70609905/combined-two-arrays-of-different-size-with-different-properties/70610915?noredirect=1#comment124824384_70610915
#>
$GroupMembersCount = $null
$GroupMembersCount = gam print groups domain <Domain.com> members managers owners countsonly | ConvertFrom-Csv
$GroupSettings = $null
$GroupSettings = gam print groups settings | ConvertFrom-Csv
$GroupsEmpty = $null
$GroupsEmpty = gam config csv_output_row_filter "directMembersCount:count=0" print groups directmemberscount | ConvertFrom-csv
$GroupMemberCountByEmail = @{}
$GroupMembersCount | ForEach-Object {
$GroupMemberCountByEmail[$_.email] = $_
}
$Result = $null
$Result = $GroupSettings | Select-Object *,
@{
Name = 'MembersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].MembersCount }
}, @{
Name = 'ManagersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].ManagersCount }
}, @{
Name = 'OwnersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].OwnersCount }
}
If ($GroupsEmpty) {
$Result | Add-Member -NotePropertyName EmptyGroup -NotePropertyValue $false
function Get-ArrayRowIndex {
param(
[parameter(mandatory = $true)][array]$Property,
[parameter(mandatory = $true)][string]$Value
)
[int]$index = 0
while ($index -lt ($Property.count)) {
if ($Property[$index] -eq $Value) {
return [int]$index
}
$index++
}
return $null
}
$Result | ForEach-Object {
If ($GroupsEmpty.email -contains $_.Email) {
$UpdateRowIndex = $null
#Get-ArrayRowIndex tries to find the item to update in the Result array. $UpdateRowIndex can't be [int]
$UpdateRowIndex = Get-ArrayRowIndex -Property $($Result.email) -Value $_.Email
#update the correct item in the array,changing flag made above.
If ($null -ne $UpdateRowIndex) {
$Result[$UpdateRowIndex].EmptyGroup = $true
}
Else {
#If location in array was not found function Get-ArrayRowIndex.
Write-Error "Problem with final report. Index location in the result array was not found using Get-ArrayRowIndex."
Start-Sleep -Seconds 3
break script
}
}
}
}
$result | Export-Csv 'c:\temp\groups.csv' -NoTypeInformation -Encoding UTF8
Try {
Invoke-Item -Path 'c:\temp\groups.csv'
}
Catch {
write-error "Final report not found at c:\temp\groups.csv"
}
There are better ways to do what I did above but this suits me for now.
Upvotes: 0
Reputation: 25001
I would just pipe to another Select-Object
to keep your code cleaner. It is not required if you want to reuse your hash table lookups in another calculated property.
$GroupSettings | Select-Object *,
@{
Name = 'MembersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].MembersCount }
},@{
Name = 'ManagersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].ManagersCount }
},@{
Name = 'OwnersCount'
Expression = { [int]$GroupMemberCountByEmail[$_.email].OwnersCount }
} |
Select-Object *,
@{
Name='AllZeroes'
Expression={-not ($_.MembersCount -or $_.ManagersCount -or $_.OwnersCount) }
} |
Export-Csv 'c:\temp\groups.csv' -NoTypeInformation -Encoding UTF8
AllZeroes
property will return $true
if all counts are 0
.
Upvotes: 3