Search code examples
arrayspowershelltexttype-conversionexport-to-csv

Powershell convert an array to CSV


$LogFile = "\\server44.socogen.com\Share01\REPORTS\Status.txt"
$Header = "\\WORKSTN\temp\Header.txt"
$Workfile = "\\WORKSTN\temp\WorkFile.csv"
$Filter = "POLICY"

$Line = Get-Content $LogFile | Where-Object {$_ -match $Filter}
$a = $Line
$a.ToString()

#$Headers = ('Session End', 'Time', 'Policy', 'Client', 'Schedule', 'Status')
$a | Export-CSV -Path $workFile #-Header "Session End","Time","Policy","Client","Schedule","Status"

I have used get-content to grab specific text from a large text file. Now I need to add headers to it and convert to CSV so I can work with it. Everything exports to column 1 with no headers or I get the following:

#TYPE Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData         
ClassId2e4f51ef21dd47e99d3c952918aff9cd formatEntryInfo outOfBand   writeStream
27c87ef9bbda4f709f6b4002fa4af63c    Microsoft.PowerShell.Commands.Internal.Format.RawTextFormatEntry    TRUE    None
27c87ef9bbda4f709f6b4002fa4af63c    Microsoft.PowerShell.Commands.Internal.Format.RawTextFormatEntry    TRUE    None
27c87ef9bbda4f709f6b4002fa4af63c    Microsoft.PowerShell.Commands.Internal.Format.RawTextFormatEntry    TRUE    None
27c87ef9bbda4f709f6b4002fa4af63c    Microsoft.PowerShell.Commands.Internal.Format.RawTextFormatEntry    TRUE    None
27c87ef9bbda4f709f6b4002fa4af63c    Microsoft.PowerShell.Commands.Internal.Format.RawTextFormatEntry    TRUE    None

The $Line variable which is an array:

$Line.gettype()

IsPublic IsSerial Name                                     BaseType                                                                
-------- -------- ----                                     --------                                                                
True     True     Object[]                                 System.Array 

Here is a sample of the contents of $Line:

07/13/2021 11:26:27  POLICY                  SERVER1                         Incremental                0
07/13/2021 11:28:13  POLICY                  SERVER2                         Incremental                0
07/13/2021 11:28:27  POLICY                  SERVER3                         Incremental                0
07/13/2021 11:28:35  POLICY                  SERVER4                         Incremental                0
07/13/2021 11:28:48  POLICY                  SERVER5                         Incremental                0
07/13/2021 11:31:51  POLICY                  SERVER6                         Incremental                0
07/13/2021 11:32:07  POLICY                  SERVER7                         Incremental                0
07/13/2021 11:32:45  POLICY                  SERVER8                         Incremental                0
07/13/2021 11:33:00  POLICY                  SERVER9                         Incremental                0
07/13/2021 11:56:52  POLICY                  SERVER10                        Incremental                0
07/13/2021 11:57:12  POLICY                  SERVER11                        Incremental                0
07/13/2021 12:05:39  POLICY                  SERVER12                        Incremental                0
07/13/2021 12:05:51  POLICY                  SERVER13                        Incremental                0
07/13/2021 12:06:05  POLICY                  SERVER14                        Incremental                0
07/13/2021 12:06:11  POLICY                  SERVER15                        Incremental                0
07/13/2021 12:12:22  POLICY                  SERVER16                        Incremental                0
07/13/2021 12:12:35  POLICY                  SERVER17                        Incremental                0
07/13/2021 12:13:12  POLICY                  SERVER18                        Incremental                0
07/13/2021 12:13:26  POLICY                  SERVER19                        Incremental                0

Solution

  • Thank you to TheMadTechnician. Your response was close enough to lead me to the answer:

    $Headers = ('Session End', 'Time', 'Policy', 'Client', 'Schedule', 'Status')
    $Line = Get-Content $LogFile | Where-Object {$_ -match $Filter}
    $a = $Line -replace ' {1,}',',' | ConvertFrom-Csv -Header $Headers
    $a | Export-Csv $WorkFile -NoTypeInfo
    $b = $a.Client | select-object -unique | sort