powershell - Comparing 2 arrays of objects properties, adding property to 1 object -
my first question here. have script 2 arrays of objects 1 property object1 match 1 property object 2. when properties match, object1 gets new property based on property in object2. range of properties can around 5-20 different ones , array holding object1 can contain 100+ objects if there better way other double loops great, still new handling/comparing objects in way.
so far have doesn't seem work.
foreach ($vm in $vms) { foreach ($csv in $csvs) { if ($vm.location -eq $csv.ownernode) {$vm = $vm | add-member -membertype noteproperty -name csv -value $csv.name} } }
edit more background, posted further down reason chose use objects need add more , more properties scrip runs, @ end use them (around 4-5 properties per object) sort them , different actions based on different properties.
if properties need objects in $csvs
ownernode
, name
properties, might speed process (and limit memory footprint) creating hashtable
use ownernode
key , name
value (assuming both properties of type string
):
$csvlookup = @{} $csvs |foreach-object { $csvlookup[$_.ownernode] = $_.name } # no need anymore remove-variable csvs foreach($vm in $vms){ if($csvlookup.containskey($vm.location)){ $vm = $vm |add-member -membertype noteproperty -name csv -value $csvlookup[$vm.location] -passthru } }
Comments
Post a Comment