Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Add multiple instances of a noteproperty to an object

$
0
0

Hello all,

I’ve got a list of ADSiteNames and Subnets. My ideal output is:

Name:       SiteName
  Subnets:       subneta
                 subnetb
                 subnetc

So that I could pipe this on to a further command and do somethning with each subnet. I can’t quite think of the formatting, so as I workaround I’ve come up with this, in which I add a SubNet$i noteproperty to the object with each iteration of a for loop. It works but is so very ugly, and working with the output later is dodgy at best.

$sites = Import-Csv -Delimiter ";" .\AdSitestoSubnets.csv
#$sites | Select-Object -Unique Site

$unique_sites = @()

ForEach ($site in ($sites | Select-Object -Unique Site)){
    
    $obj = New-Object PSObject -Property @{
            Name   = $site.site
            }
    $i = 0        
    $sites | Where-Object Site -eq $site.Site | ForEach-Object {
        $obj | Add-Member -NotePropertyName "SubNet$i" $_.SubNet
        $i++
        }

        $unique_sites += $obj
        
    }


Name     : USLouisville
SubNet0  : 10.1.128.0
SubNet1  : 10.14.0.0
SubNet2  : 10.16.0.0
SubNet3  : 10.16.10.0
SubNet4  : 10.16.10.0
SubNet5  : 10.16.100.0
SubNet6  : 10.16.90.0
SubNet7  : 10.220.16.0
SubNet8  : 10.220.18.0
SubNet9  : 10.220.20.0
SubNet10 : 10.220.46.0
SubNet11 : 10.221.38.0
SubNet12 : 10.3.0.0

Name    : USHouston
SubNet0 : 10.1.137.0
SubNet1 : 10.220.188.0
SubNet2 : 10.220.49.0


If you have to do this task, how would you do it? I’d consider my solution to be very, very ugly, even if it works. I’m hoping you guys can show me a new construct or method of doing this.


Viewing all articles
Browse latest Browse all 13067

Trending Articles