Search code examples
arraysjsonpowershellloopspsobject

Looping through properties inside a PSObject in PowerShell (from json)


After opening a .json file in PowerShell, I've got a new PSObject called $json_sitesandsnets. Ultimately what I want to do is loop through each Site (Orlando, Denver), and loop through each of their subnets, and do stuff. Initially, I need to create these subnets for the corresponding Site in Active Directory with the New-ADReplicationSubnet cmdlet. I've already used this to create a single subnet, but I've got a much larger list of Sites & Subnets I'll be working with. This example is just a small trimmed down version to post here. Once I figure out how to access those subnets in the objects Value, I might just feed each subnet list to New-ADReplicationSubnet. But I really want to be able to loop through each subnet for other purposes. I'm thinking that this is probably not difficult to do, once you know how to do it.

Here is the raw json:

{
"Orlando":  [
                        "10.10.10.0/24",
                        "10.10.20.0/24",
                        "10.10.30.0/24",
                        "10.10.40.0/24"
                    ],
"Denver":  [
                       "10.0.70.0/24",
                       "10.0.80.0/24",
                       "10.0.90.0/24",
                       "10.0.100.0/24",
                       "10.0.110.0/24",
                       "10.0.120.0/24"
                   ]
}

And I get the PSObject's properties like this:

$json_sitesandsnets.psobject.properties

PSObject properties

When I run Debug in VSCode, I can see the PSObject and the list (array) of subnets: seeing this in Debug

Here is some code, which gets the Name no problem (Write-Host $site.Name). As you can see, I've tried a couple of different ways to access those subnets, which are the Value of each site. And I just cannot figure it out. I've read so many articles and forum threads about looping through PSObjects, but just can't seem to find the golden answer to accessing and looping through the array that is the Value of the PSObject. I feel like I'm really close.

foreach ($site in $json_sitesandsnets.PSObject.Properties) {
Write-Host $site.Name
$site| get-member -type NoteProperty | foreach-object {
    $subnet = $site."$($_.Value)"
    Write-Host $subnet
    Write-Host $_.Value
}
}

Solution

  • It looks like to me that what you want is this.

    foreach ($site in $json_sitesandsnets.PSObject.Properties) {
        Write-Host $site.Name -ForegroundColor Cyan
        $site.Value | Out-String | Write-Host 
    }
    

    Output

    Output #1

    For the subnet values, the not properties are Orlando and Denver. You got that part right however your $site| get-member line shouldn't be there. Instead, you can access the value through $site.value directly. If you needto do something more, you can loop through these values, like this:

    $index = 0
    Foreach ($subnet in $site.Value) {
        Write-Host "$($index): $subnet"
        $index+=1
    }
    

    Output

    Output #2