powercli / powershell import csv and nested loops -
i have csv file multiple column select 2 , 3 (just ignore 1st one) goal execute command "hash" parameter each host. each host have 2+n hash options need find way execute n+1 times same command different "hash" parameter.
my goal logic is:
connect esx1 execute "command -option 9221" execute "command -option 53301" connect esx12 execute "command -option 55799" execute "command -option 51990" ... etc
the problem have hostname in each line , results below can see loop again , again same host executing same "hash" commands
csv content magic hostname hash 3 esx1.mylab.local 9221 3 esx1.mylab.local 53301 3 esx12.mylab.local 55799 3 esx12.mylab.local 51990 3 esx15.mylab.local 62157 3 esx15.mylab.local 12796 $import = import-csv c:\mycsv.csv | select hostname,hash foreach ($vmhost2 in $import.hostname){ write-host "connecting $vmhost2" foreach ($myhash in $import.hash) { write-host "executing magic $vmhost2 $myhash" } }
result
>>connecting esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local connecting esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local executing magic esx1.mylab.local connecting esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local connecting esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local executing magic esx12.mylab.local connecting esx15.mylab.local executing magic esx15.mylab.local executing magic esx15.mylab.local executing magic esx15.mylab.local executing magic esx15.mylab.local executing magic esx15.mylab.local executing magic esx15.mylab.local
any idea of logic. tried create csv uniq hostnames , leave "hash" column hit "cannot connect empty"
csv content magic hostname hash 3 esx1.mylab.local 9221 3 53301 3 esx12.mylab.local 55799 3 51990 3 esx15.mylab.local 62157 3 12796
you need group
command!
$import = import-csv c:\mycsv.csv | select hostname,hash foreach ($vmhost2 in ($import|group hostname)){ write-host "connecting $($vmhost2.name)" foreach ($myhash in $vmhost2.group.hash) { write-host "executing magic $($vmhost2.name) $myhash" } }
that, sample csv, output this:
connecting esx1.mylab.local executing magic esx1.mylab.local 9221 executing magic esx1.mylab.local 53301 connecting esx12.mylab.local executing magic esx12.mylab.local 55799 executing magic esx12.mylab.local 51990 connecting esx15.mylab.local executing magic esx15.mylab.local 62157 executing magic esx15.mylab.local 12796
wiki
Comments
Post a Comment