I’m trying to create a report in Powershell but running into a problem when trying to export. Basically I have a loop that is adding the data to an array, however when I go to export all the data isn’t exporting. I broke it out of the loop so I can show you what’s happening.
What I'm expecting:
+----------+-----------+--------+
| HostName | Platform | 3rdCol |
+----------+-----------+--------+
| Test1 | Platform1 | |
| Test2 | Platform2 | 3rdCol |
+----------+-----------+--------+
What I'm getting:
+----------+-----------+
| 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
Export-Csvinfers the column headers from the first object that is piped to it. Instead of only adding the3rdColproperty to some objects, add it to all objects (just set it to$nullfor objects that don't need it)