Хорошо, отделив отличный ответ от Joey, для итерации по массиву с известными столбцами вы можете сделать следующее (PowerShell v3 или выше, это можно сделать в более старых версиях, но усложняется).
#clean up any existing variables
Remove-Variable array, alliterations -ea 4
#create the sample array
[array]$array += [PSCustomObject]@{
'Column 1' = 'Single load'
'Column 2' = 'MAT1'
'Column 3' = 'Country A'
}
[array]$array += [PSCustomObject]@{
'Column 1' = 'Uniform distributed load'
'Column 2' = 'MAT2'
'Column 3' = 'Country B'
}
[array]$array += [PSCustomObject]@{
'Column 1' = ''
'Column 2' = 'MAT3'
'Column 3' = 'Country C'
}
#Create all iterations from that array
ForEach($a in ($array.'Column 1'|Where{$_})){
ForEach($b in ($array.'Column 2'|Where{$_})){
ForEach($c in ($array.'Column 3'|Where{$_})){
[array]$AllIterations += [PSCustomObject]@{
'Column 1' = $a
'Column 2' = $b
'Column 3' = $c
}
}
}
}
#Display results
$AllIterations
Это будет генерировать массив:
Column 1 Column 2 Column 3
-------- -------- --------
Single load MAT1 Country A
Single load MAT1 Country B
Single load MAT1 Country C
Single load MAT2 Country A
Single load MAT2 Country B
Single load MAT2 Country C
Single load MAT3 Country A
Single load MAT3 Country B
Single load MAT3 Country C
Uniform distributed load MAT1 Country A
Uniform distributed load MAT1 Country B
Uniform distributed load MAT1 Country C
Uniform distributed load MAT2 Country A
Uniform distributed load MAT2 Country B
Uniform distributed load MAT2 Country C
Uniform distributed load MAT3 Country A
Uniform distributed load MAT3 Country B
Uniform distributed load MAT3 Country C
Чтобы получить, что в Excel вы можете экспортировать в файл CSV, как предложил Джо, или вы можете иметь PowerShell открытые Excel и вставить в него данные непосредственно.
#Copy array to clipboard as a tab delimited CSV
$AllIterations | ConvertTo-CSV -Delimiter "`t" -NoTypeInfo | Select -Skip 1 | Clip
#Open Excel, create a blank workbook, and paste the data in at cell A1
$XL = New-Object -ComObject Excel.Application
$WB = $XL.Workbooks.Add()
$XL.Visible = $true
$XL.ActiveSheet.Cells.Item(1).PasteSpecial()
Это массив объектов (есть ли заголовки столбцов?) Или массив массивов (без заголовков столбцов)? – TheMadTechnician
@ TheMadTechnician, Да, я забыл поставить заголовки. Столбец 1, столбец 2 и столбец 3. – rock