我被困在一个点,我有两个像这样的数组变量
$name = @("A","B","C")
$roll = @("1","2","3")
我正在尝试使用此数据制作csv文件。我正在使用此代码
$name = @("A","B","C")
$roll = @("1","2","3")
$report = New-Object psobject
$report | Add-Member -MemberType NoteProperty -Name "Name" -Value "$name"
$report | Add-Member -MemberType NoteProperty -Name "Roll" -Value "$roll"
$report | Export-Csv -Path "C:\Users\Abby\Desktop\r.csv" -NoTypeInformation
但我有这样的输出
NAME |辊
A B C | 1 2 3
我希望我的输出像这样
NAME |辊
A | 1
B | 2
C | 3
任何人都知道怎么做这些......请帮助我...... :(
答案 0 :(得分:2)
您当前的代码正在创建对象,并将数组$name
和$roll
添加为单个条目。
听起来你想要一个数组中每个值的条目。假设数组长度相同,您可以迭代值:
$name = @("A","B","C")
$roll = @("1","2","3")
foreach($i in 0..($name.Count -1)){
[array]$report += [pscustomobject] @{
Name = $name[$i]
Roll = $roll[$i]
}
}
# screen output
# $report | Format-Table
$report | Export-Csv -Path "C:\Users\Abby\Desktop\r.csv" -NoTypeInformation
屏幕输出
Name Roll
---- ----
A 1
B 2
C 3
答案 1 :(得分:1)
试试这个:
$name = "A","B","C"
$roll = "1","2","3"
$index = 0
$name | %{[pscustomobject]@{Name=$_;Roll=$roll[$index++]}} |Export-csv "C:\Users\Abby\Desktop\r.csv" -NoType