I have an array of hashtables ($workObjectFromFile) that contain a single string 'id' and an array 'roles'. I want to return each row/object in the ht whose roles array matches an input array.
# generate workobject [System.Collections.Hashtable]
$workObject = @()
$workObject += @{id = 'server1'; roles = 'role1'}
$workObject += @{id = 'server2'; roles = 'role1','role2'}
$workObject += @{id = 'server3'; roles = 'role4','role5','role2'}
$json_string = ConvertTo-Json $workObject -Depth 20
$jsonFile = "C:\scratch\hostmap.json"
Set-Content $jsonFile -Value $json_string -Force
$json_fromFile = Get-Content $jsonFile -Raw
$workObjectFromFile = ConvertFrom-Json $json_fromFile
# find and return each server with any one of the given roles in the input filter
$inputFilter = 'role1','role3'
$return = @()
$workObjectFromFile | where {$_.roles -in $inputFilter} | foreach {
$return += [ordered]@{
id = $_.id
roles = $_.roles
}
}
return $return
Why does the where clause above only return server1? if i change this to
$workObjectFromFile.roles | where {$_ -in $inputFilter}
It will return only the roles, but for the correctly matched objects
I need to return the parent object for these matched nested arrays