Avatar of Sam Jacobs
Sam JacobsFlag for United States of America

asked on 

How can I extract information from grouped records?

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='192.168.4.1';subnet='192.168.4.x'})
$users += [PSCustomObject]([ordered]@{Name='Tony';IP='192.168.5.20';subnet='192.168.5.x'})
$users += [PSCustomObject]([ordered]@{Name='Cheryl';IP='192.168.4.1';subnet='192.168.4.x'})
$users += [PSCustomObject]([ordered]@{Name='Bob';IP='192.168.3.5';subnet='192.168.3.x'})
$users += [PSCustomObject]([ordered]@{Name='Sandy';IP='192.168.5.20';subnet='192.168.5.x'})

$users

Open in new window

This creates my array of users:
User generated image
Grouping the above by Subnet and IP should give me a report something like this:
User generated image
So I use the Group-Object cmdlet:
$counts = $users | group subnet, IP
$counts

Open in new window

and that produces this:
User generated image
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)"

Open in new window


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

Open in new window


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}}
    }
}

Open in new window


I don't get any output.
What am I doing wrong?
Thanks.
Powershell

Avatar of undefined
Last Comment
Sam Jacobs
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

ASKER

Thank you!
Powershell
Powershell

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework. PowerShell provides full access to the Component Object Model (COM) and Windows Management Instrumentation (WMI), enabling administrators to perform administrative tasks on both local and remote Windows systems as well as WS-Management and Common Information Model (CIM) enabling management of remote Linux systems and network devices.

27K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo