Reputation: 45
I am struggling exporting my list of objects to a csv-file. I have the following have the following code so far:
$accessListPath = "example.cfg"
$Outputfile = "ScriptOutput.csv"
$accessList = @()
$xml = [XML](Get-Content -Path $accessListPath)
$xml | Select-Xml -xpath "//value" | %{
$accessList += New-Object -TypeName PSObject -Property @{
Rolle = $_.Node.roll
Gruppe = $_.Node.group
ADString = $_.Node."#text".trim()
}
}
Export-Csv -InputObject $accessList -Path $Outputfile
The corresponding XML file is this:
<?xml version="1.0" encoding="UTF-8"?>
<ldap>
<host>x.x.x.x
<users>
<user>DC=Example,DC=internal</user>
</users>
<rights>
<var>distinguishedName
<value>CN=...,OU=user,OU=...
<roll>2</roll>
</value>
<value>CN=...,OU=user,OU=...
<roll>5</roll>
<roll>18</roll>
</value>
<value>CN=John Doe*
<roll>9</roll>
</value>
<value>CN=Domain Admin*
<group>Administrator</group>
<roll>1</roll>
</value>
<value>CN=...,OU=user,...
<group>Example Group</group>
<roll>8</roll>
<roll>12</roll>
<roll>14</roll>
<roll>15</roll>
</value>
</var>
<var>search:member=<userdn>
<value>CN=group1*
<group>01 Group XY</group>
<roll>1</roll>
</value>
<value>CN=Client1,OU=*
<roll>3</roll>
</value>
<value>CN=...,OU=*
<roll>5</roll>
</value>
<value>CN=ImportantClient06*
<group>06ImportantGroup</group>
<roll>8</roll>
<roll>11</roll>
<roll>12</roll>
</value>
</var>
</rights>
</host>
This is the Output in the .csv file:
TYPE System.Object[] Count Length LongLength Rank SyncRoot IsReadOnly IsFixedSize IsSynchronized 49 49 49 1 System.Object[] False True False
Upvotes: 3
Views: 4816
Reputation: 11
The answer on this post helped solve my challenge. In lieu of the piped "| Export-CSV" I tried "| Out-String" which resulted for me in xml text again in my table DataAccessPlan-f51da2a1-8c5c-496c-aa44-46a259b1471a~.xml ...
Below my code. it gets the name of a file and shows the content next to it.
$table = New-Object System.Data.Datatable
[void]$table.Columns.Add("Name")
[void]$table.Columns.Add("Content")
$source="D:\test\DataAccessPlans" #location of starting directory
$files=@("*.xml") #if you want to include extensions add -include ($files) to get-ChildItem
forEach($item in $(Get-ChildItem -recurse ($source) -File -include $files) ){
$currentContent = Get-Content -Path $item
$currentContent | Out-String
[void]$table.Rows.Add($item.Name,$($currentContent | Out-String))
}
$table | Export-Csv -Path "D:\export.csv"
Upvotes: 1
Reputation: 54941
Try:
$accessList | Export-Csv -Path $Outputfile
When you specify -InputObject $accessList
you're saying use THIS object. That actually exports the array, which is what you're seeing. When you use $accessList | Export-CSV ...
, it sends the items inside the array down the pipeline so all the items are exported.
Also, you could add -NoTypeInformation
to remove the TYPE ...
-line.
Upvotes: 3