Powershell, nested queries

Just a quick one.

In a previous post, I mentioned I love using hash-tables, usually organised into complex collections.

Today I had to perform a "nested query" on a hash-table that contained nested hash-tables.

What I wanted, was a filtered collection of nodes based upon (the top-level nodes) matches found by a sub-query of their audiences child node.

In English, I wanted all the configs where they had support for private developer environments.

After I have my list, I wanted the one with the "lowest cost".

Here it is...


$configs = (
    @{
        name = “aws”
        audiences = (
            @{
                name = “dev”
                limit = 999
                scope = ,“private”},
            @{
                name = “test”
                limit = 999
                scope = ,“private”},
            @{
                name = “uat”
                limit = 999
                scope = “public”,“private”}
        )
        cost = 100
    },
     @{
        name = “inhouse”
        audiences = (
            @{
                name = “dev”
                limit = 2
                scope = "public",“private”},
            @{
                name = “test”
                limit = 2
                scope = ,“private”}
        )
        cost = 500
    }
   )
   
$sample = $configs | where {$_.audiences | where {$_.name -eq "dev" -and $_.scope -contains "private"}}


$sample.GetEnumerator() |  Sort-Object {$_.cost}  | Select-Object -first 1

Comments