我試圖在Powershell中創建報告,但試圖導出時遇到問題。基本上我有一個循環,即將數據添加到數組,但是當我去導出所有的數據不導出。我把它打破了循環,所以我可以告訴你發生了什麼。具有不同列數的導出數組
我很期待:
+----------+-----------+--------+
| HostName | Platform | 3rdCol |
+----------+-----------+--------+
| Test1 | Platform1 | |
| Test2 | Platform2 | 3rdCol |
+----------+-----------+--------+
什麼我越來越:
+----------+-----------+
| HostName | Platform |
+----------+-----------+
| Test1 | Platform1 |
| Test2 | Platform2 |
+----------+-----------+
PowerShell的:
#Create report array
$report = @()
#New object for Test1
$stats = New-object PSCustomObject
#Hostname column for Test1
$stats | Add-Member -Name HostName -Value Test1 -Membertype NoteProperty
#Platform column for Test1
$stats | Add-Member -Name Platform -Value Platform1 -Membertype NoteProperty
#Add row to report
$report += $stats
#New object for Test2
$stats = New-object PSCustomObject
#Hostname column for Test2
$stats | Add-Member -Name HostName -Value Test2 -Membertype NoteProperty
#Platform column for Test2
$stats | Add-Member -Name Platform -Value Platform2 -Membertype NoteProperty
#3rd column for Test2
$stats | Add-Member -Name 3rdCol -Value 3rdCol -Membertype NoteProperty
#Add row to report
$report += $stats
#Export only has two columns instead of 3
$report | export-csv c:\temp\report.csv -notypeinformation
'出口-Csv'從推斷列標題*** ***先傳送給它的對象。代替僅將'3rdCol'屬性添加到* some *對象,將其添加到* all *對象(只需將它設置爲'$ null',以便不需要它的對象) –
@ MathiasR.Jessen將在此工作例如,但是在循環中,我動態地創建了列,並且不知道將會有多少列。 – solar411
您能向我們展示您的實際腳本嗎? –