I'm trying to use Group-Object to group a list of objects based on a custom computation. A very simplified example is the following:
$groupedProjects = 1..10 | %{
[PSCustomObject]@{
ErrorMessage = if ($_ % 2) { 'SomeError' } else { '' }
}
} | Group-Object -Property { if ($_.ErrorMessage) { 'Failed' } else { 'Successful' } } -AsHashTable
$successGroup = $groupedProjects['Successful']
# $successGroup is null here instead of a list with the integers.
The problem is that the groups are not keyed by the string but by something else instead - not sure I really understand the problem.
PS: Using a script block is shown in the documentation in this manner:
1..35 | Group-Object -Property {$_ % 2},{$_ % 3}
so I'd assume the idea is valid - using integers instead of strings also works as intended. If there's a better way to do the above I'm all ears though.