首页 > 解决方案 > 如何在 Powershell 中加入两个对象数组

问题描述

我的 powershell 脚本中有两个对象数组

第一个有这样的价值观

@{Id=1, Count=24}
@{Id=2, Count=34}

第二

@{Id=1, Name="Some name"}
@{Id=2, Name="Some other name"}

我喜欢加入这两个所以我得到

@{Id=1, Count=24 Name="Some name"}
etc

最终我需要将它写入一个 csv 文件。

标签: powershell

解决方案


使用此Join-Objectcmdlet(另请参阅:在 Powershell 中,将两个表合并为一个的最佳方法是什么?)其中(自定义)对象(包括数据表)和哈希表:

$Array1 = @{Id=1; Count=24},
          @{Id=2; Count=34}
    
$Array2 = @{Id=1; Name="Some name"},
          @{Id=2; Name="Some other name"}

$Array1 | Join $Array2 -On Id

Count Id Name
----- -- ----
   24  1 Some name
   34  2 Some other name

推荐阅读