首页 > 解决方案 > 将数组的列值更改为列标题

问题描述

我正在从 API 中提取一些数据,这些数据最终以两个字符串的形式存储,如下所示:

$String1

客户 1
客户 2
客户 3

$String2

产品一
产品b
产品c

以上是现实中的一长串清单。

我还从存储在数组中的不同 API 中获取了一组不同的数据。

$Array

Server     State
abcd.com   Normal
defg.com   Dead
fguy.com   Normal

请注意,我从提取为和的不同 API 输出中获取$String1$String2是 foreach 的值的方式。findstr Clientfindstr Product

现在,我想从这 3 个 $s 中获得一个看起来像这样的决赛桌:

$Final

Server     State   Client   Product
abcd.com   Normal    1         a
defg.com   Dead      2         b
fguy.com   Normal    3         c

所以我首先尝试做的是创建和中间表,它可能看起来像

Client   Product
1          a
2          b
3          c

然后与 合并$Array到决赛桌。

目前我对此无处可去,我尝试了很多不同的方法,这些方法看起来很愚蠢,让我无处可去。

标签: arrayspowershellmerge

解决方案


由于它看起来像$Array3一个具有两个属性的对象数组:Serverand State,我认为这可以帮助您:

$Array1 = 'Client  1','Client  2','Client  3'
$Array2 = 'Product a','Product b','Product c'
$Array3 = @(
    [PsCustomObject]@{'Server' = 'abcd.com'; 'State' = 'Normal'},
    [PsCustomObject]@{'Server' = 'defg.com'; 'State' = 'Dead'},
    [PsCustomObject]@{'Server' = 'fguy.com'; 'State' = 'Normal'}
)

for ($i = 0; $i -lt $Array3.Count; $i++) {
    $Array3[$i] | Select-Object *, 
                                @{Name = 'Client'; Expression = { $Array1[$i] -replace '^Client\s*'}},
                                @{Name = 'Product'; Expression = { $Array2[$i] -replace '^Product\s*'}}
}

输出:

Server   State  Client Product
------   -----  ------ -------
abcd.com Normal 1      a      
defg.com Dead   2      b      
fguy.com Normal 3      c

如果您愿意,您可以捕获 for(..) 循环的结果并将其保存为 CSV 文件某处。在这种情况下,只需做

$result = for ($i = 0; $i -lt $Array3.Count; $i++) {
    $Array3[$i] | Select-Object *, 
                                @{Name = 'Client'; Expression = { $Array1[$i] -replace '^Client\s*'}},
                                @{Name = 'Product'; Expression = { $Array2[$i] -replace '^Product\s*'}}
}
$result | Export-Csv -Path 'D:\serverresult.csv' -NoTypeInformation


更新
显然,您提到的数组 1 和 2 根本不是数组,而是(多行)字符串。

在这种情况下,拆分这些字符串中的行,这样您最终将得到真正的数组:

$string1 = @"
Client  1
Client  2
Client  3
"@

$string2 = @"
Product  a
Product  b
Product  c
"@

# split the multiline strings (examples) on the Newline character into arrays
$Array1 = $string1 -split '\r?\n'
$Array2 = $string2 -split '\r?\n'

# now, both arrays will have 3 elements:
# $Array1 = 'Client  1','Client  2','Client  3'
# $Array2 = 'Product a','Product b','Product c'

# array no. 3 is an array of objects as we saw earlier
$Array3 = @(
    [PsCustomObject]@{'Server' = 'abcd.com'; 'State' = 'Normal'},
    [PsCustomObject]@{'Server' = 'defg.com'; 'State' = 'Dead'},
    [PsCustomObject]@{'Server' = 'fguy.com'; 'State' = 'Normal'}
)


# Finally, you can use the `for(..)` loop unaltered

$result = for ($i = 0; $i -lt $Array3.Count; $i++) {
    $Array3[$i] | 
        Select-Object *, 
                      @{Name = 'Client'; Expression = { $Array1[$i] -replace '^Client\s*'}},
                      @{Name = 'Product'; Expression = { $Array2[$i] -replace '^Product\s*'}}
}

# output on console
$result

# output to CSV file
$result | Export-Csv -Path 'D:\serverresult.csv' -NoTypeInformation

推荐阅读