powercli / powershell导入csv和嵌套循环

时间:2017-08-21 17:58:01

标签: powershell powercli

我有多个列的CSV文件,我从中选择2和3(只是忽略第一个) 我的目标是用" hash"执行命令。每个主机的参数。每个主机都有2 + n个哈希选项,所以我需要找到一种方法来执行n + 1次相同的命令,使用不同的"哈希"参数。

我的目标逻辑是:

connect to esx1 
execute "command -option 9221" 
execute "command -option 53301"

connect to esx12
execute "command -option 55799"
execute "command -option 51990"  

... etc

问题是我在每一行都有主机名,从下面的结果中你可以看到我一次又一次地从同一主机执行相同的循环#34; hash"命令

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 

任何逻辑的想法。我尝试使用uniq主机名创建CSV并且只留下" hash"列然后我点击#34;无法连接到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

1 个答案:

答案 0 :(得分:3)

您需要Group命令!

$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"
    }
 }

使用您的示例CSV输出:

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