Since it looks like $Array3
is an array of objects with two properties: Server
and State
, I think this could help you out:
$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*'}}
}
Output:
Server State Client Product
------ ----- ------ -------
abcd.com Normal 1 a
defg.com Dead 2 b
fguy.com Normal 3 c
If you want to, you can capture the result of the for(..) loop and save that as CSV file somewhere. In that case just do
$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
Update
Apparently the arrays 1 and 2 you mention are not arrays at all, but (multiline) strings.
In that case, split the lines inside these strings so you will end up with true arrays:
$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