3

Hi I'm trying to count number of arrow where two value are matched.

Here is how i put value in my array :

            $obj | add-member -name "Site" -membertype Noteproperty -value $sitecode
            $obj | add-member -name "Location" -membertype Noteproperty -value $location
            $obj | add-member -name "SwitchName" -membertype Noteproperty -value $swname
            $obj | add-member -name "MacAddress" -membertype Noteproperty -value $mac
            $obj | add-member -name "Interface" -membertype Noteproperty -value $eth
            $obj | add-member -name "Macvendor" -membertype Noteproperty -value ""
            $obj | add-member -name "IP" -membertype Noteproperty -value ""
            $tab += $obj

Now i would like to count each combibation of SwitchName / Interface.

Exemple my arrays can contains the following amongt other value :

Switchname=foo,portX
Switchname=foo,portY
Switchname=foo,portX
Switchname=foo,portZ
Switchname=bar,portX
Switchname=bar,portZ
Switchname=bar,portT

I want to be able to get :

Switchname=foo,portX,2
Switchname=foo,portY,1
Switchname=foo,portZ,1
Switchname=bar,portX,1
Switchname=bar,portZ,1
Switchname=bar,portT,1

I'm currently thinking about using another table to count all of this. Any easier way to do that?

Thanks.

2
  • 1
    $tab | group SwitchName, Interface | % { @($_.Values; $_.Count) -join ',' } Commented Feb 1, 2017 at 17:10
  • That's working, i knew it could be something "simple". Thanks. Commented Feb 2, 2017 at 16:49

2 Answers 2

1

Use the Group-Object cmdlet:

PS C:\> $tab |Group-Object -Property SwitchName,Interface -NoElement

Count Name                     
----- ----                     
    2 foo, portX               
    1 foo, portY               
    1 foo, portZ               
    1 bar, portX               
    1 bar, portZ               
    1 bar, portT               
Sign up to request clarification or add additional context in comments.

1 Comment

That's working too, i knew it could be something "simple". Thanks.
0

try Something like this:

$tab | select SwitchName, Interface , @{N="Number";E={$switchname=$_.SwitchName;$interface=$_.Interface; ($tab | where {$_.SwitchName -eq $switchname -and $_.Interface -eq $interface }).Count}}

1 Comment

That's working too, the output is a bit "messier" than the other response but it works ! Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.