2

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
Eazy Snatch
  • 61
  • 1
  • 1
  • 4

1 Answers1

3

You need the Group command!

$import = Import-Csv c:\mycsv.csv | select hostname,hash 
foreach ($vmhost2 in ($import|Group hostname)){
    Write-Host "Connecting to $($vmhost2.name)"
    foreach ($myhash in $vmhost2.group.hash) 
    {
        Write-Host "Executing magic for $($vmhost2.Name) with $myhash"
    }
 }

That, with your sample CSV, output this:

Connecting to esx1.mylab.local
Executing magic for esx1.mylab.local with 9221
Executing magic for esx1.mylab.local with 53301
Connecting to esx12.mylab.local
Executing magic for esx12.mylab.local with 55799
Executing magic for esx12.mylab.local with 51990
Connecting to esx15.mylab.local
Executing magic for esx15.mylab.local with 62157
Executing magic for esx15.mylab.local with 12796
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Thank you I will test that tomorrow – Eazy Snatch Aug 21 '17 at 22:30
  • If this solution worked for you, please consider marking the answer as accepted (there should be an option for that on the left just below the voting buttons). This helps provide closure, and lets future users know there's a solution if they have the same problem and go searching for a solution. – TheMadTechnician Aug 22 '17 at 16:49