1

I have a variable that contains the following:

Site                      IP

walmart                   10.20.30.40
walmart                   10.20.30.41
walmart                   10.20.30.42
target                    10.20.30.50
target                    10.20.30.51
hm                        10.20.30.60

This is an object created using New-Object psobject -Property @{ site = <code> ; IP = <code> }. So if I call $obj.site, it does list only the sites and same goes with IP.

I want to convert this is into an arraylist grouped by the sites.

For instance, result output should look like this:

walmart     :  {10.20.30.40, 10.20.30.41, 10.20.30.42}
target      :  {10.20.30.50, 10.20.30.51}
hm          :  {10.20.30.60}

I want to be able to call this from my script like for reasons like adding or subtracting IPs on any site. For instance:

$myArray.target 

Which should list the IPs like:

10.20.30.50
10.20.30.51

Instead of the same format - {10.20.30.50, 10.20.30.51}

Then I would add more IPs to these object using the Add method.

($myArray.walmart).Add("10.20.30.44")

The closest I've been able to get is the following (and its output):

$obj | Group-Object -Property Site | Select-Object Name,Group

Name            Group

walmart         {@{Site=walmart; IP=10.20.30.40}, @{Site=walmart; IP=10.20.30.41}, @{Site=walmart; IP=10.20.30.42}
target          {@{Site=target; IP=10.20.30.50}, @{Site=target; IP=10.20.30.51}
hm              {@{Site=hm; IP=10.20.30.60}

With that output, I can't call on particular site like $myArray.walmart.

I'm pretty new to powershell, what am I missing? Any help would be much appreciated!

1
  • have you looked at using a hashtable? that lets you have a unique key [the site] and a value that can be a singleton, OR an array. it makes looking up site info quite quick ... $LookupTable['SiteName'] will give you all the IPs. then $LookupTable['SiteName'][0] would give you the 1st item in the array of items in the value of that key. Commented Oct 28, 2018 at 6:41

1 Answer 1

1

try Something like this:

$Array=@(
[pscustomobject]@{Site='walmart'; ID='10.20.30.40'}
[pscustomobject]@{Site='walmart'; ID='10.20.30.41'}
[pscustomobject]@{Site='walmart'; ID='10.20.30.42'}
[pscustomobject]@{Site='target'; ID='10.20.30.50'}
[pscustomobject]@{Site='target'; ID='10.20.30.51'}
[pscustomobject]@{Site='hm'; ID='10.20.30.60'}
)

$Object=New-Object PSObject

$Array | group Site | %{

#create an new list with all id for the curretn group
$CurrentList = New-Object System.Collections.Generic.List[System.Object]
$_.group.ID | %{$CurrentList.Add($_)}

#Add property and list to the final object
Add-Member Noteproperty -Name $_.Name -value $CurrentList -InputObject $Object
}

#add element to a site
$Object.hm.Add("NewID")
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Esperento57! That helped a lot!

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.