连接2个数组

时间:2018-11-09 12:16:06

标签: powershell csv

我有如下2个数组中的数据

$csvCreate:

Server Names Services Name
------------ --------------
abc           mn
123           OK
$csvCreate1:

Server Names Mounts  
------------ ------  
abc          OK      
123          /etc/999

需要将数据附加到csv中,如下所示

Server Names Services Name Mounts  
------------ ------------- -------  
abc            mn            OK      
123            OK           /etc/999

尝试做:

   $csvCreate = $csvCreate | Select-Object ServerName,Services
   $csvCreate1 = $csvCreate1 | Select-Object Mounts
   $finalData = $csvCreate +$csvCreate1
   $finalData | Export-Csv -Path $path\ComparedServicess.csv -NoTypeInformation

但是似乎没有用。

请让我知道我在这里想念的东西

2 个答案:

答案 0 :(得分:1)

使用以下代码完成

$JoinedObject = Foreach ($row in $csvCreate)
{
    [pscustomobject]@{
        Servername = $row.ServerName
        Services     = $row.Services
        Mounts    = $csvCreate1 | Where-Object {$_.Servername -eq $row.Servername} | Select-Object -ExpandProperty Mounts
    }
}

答案 1 :(得分:0)

如果每个列表中的行数相同且顺序相同,则简单的for循环即可:

$finalData = $csvCreate | Select-Object *, Mounts

for ($i=0; $i -lt $finalData.length; $i++) {
    $finalData[$i].Mounts = $csvCreate1[$i].Mounts
}