troubleshooting Question

How can I extract information from grouped records?

Avatar of Sam Jacobs
Sam JacobsFlag for United States of America asked on
2 Comments1 Solution18 ViewsLast Modified:
I would like to create a report of the number of users coming in from a particular IP address and their subnet. Consider the following test data:
$users = @()
$users += [PSCustomObject]([ordered]@{Name='Adam';IP='';subnet='192.168.4.x'})
$users += [PSCustomObject]([ordered]@{Name='Tony';IP='';subnet='192.168.5.x'})
$users += [PSCustomObject]([ordered]@{Name='Cheryl';IP='';subnet='192.168.4.x'})
$users += [PSCustomObject]([ordered]@{Name='Bob';IP='';subnet='192.168.3.x'})
$users += [PSCustomObject]([ordered]@{Name='Sandy';IP='';subnet='192.168.5.x'})

This creates my array of users:
Grouping the above by Subnet and IP should give me a report something like this:
So I use the Group-Object cmdlet:
$counts = $users | group subnet, IP
and that produces this:
Now, I can do this:
Write-Host "$($Counts[0].Group[0].Subnet)       $($Counts[0].Group[0].IP)      $($counts[0].Count)"
Write-Host "$($Counts[1].Group[0].Subnet)       $($Counts[1].Group[0].IP)      $($counts[1].Count)"
Write-Host "$($Counts[2].Group[0].Subnet)       $($Counts[2].Group[0].IP)      $($counts[2].Count)"

which will correctly output what I expect:
192.168.4.x        2
192.168.5.x      2
192.168.3.x        1

But if I do this (which I would expect is the same thing):

 $counts | ForEach {
    Select -Property {
            @{n='Subnet'; e={$_.Group[0].Subnet}},
			@{n='IP Address'; e={$_.Group[0].IP}},
			@{n='# Users'; e={$_.Group[0].Count}}

I don't get any output.
What am I doing wrong?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros