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!
$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.