I have CSV file with multiple column from which I select 2 and 3 (just ignore 1st one) My goal is to execute command with "hash" parameter for each host. each host have 2+n hash options so I need to find a way to execute n+1 times same command with different "hash" parameter.
My goal logic is:
connect to esx1
execute "command -option 9221"
execute "command -option 53301"
connect to esx12
execute "command -option 55799"
execute "command -option 51990"
... etc
The problem is I have hostname in each line and from results below you can see I loop again and again from 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 to $vmhost2"
foreach ($myhash in $import.hash) {
Write-Host "Executing magic for $vmhost2 with $myhash"
}
}
RESULT
>>Connecting to esx1.mylab.local
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Connecting to esx1.mylab.local
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Executing magic for esx1.mylab.local with
Connecting to esx12.mylab.local
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Connecting to esx12.mylab.local
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Executing magic for esx12.mylab.local with
Connecting to esx15.mylab.local
Executing magic for esx15.mylab.local with
Executing magic for esx15.mylab.local with
Executing magic for esx15.mylab.local with
Executing magic for esx15.mylab.local with
Executing magic for esx15.mylab.local with
Executing magic for esx15.mylab.local with
Any idea of the logic. I tried to create CSV with uniq hostnames and leave just "hash" column but then I hit "cannot connect to 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