Powershell 将数组转换为 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"
我已经使用 get-content 从大型文本文件中抓取特定文本。现在我需要向它添加 headers 并转换为 CSV,以便我可以使用它。所有内容都导出到没有 headers 的第 1 列,或者我得到以下内容:
#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
作为数组的 $Line 变量:
$Line.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
这是 $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
感谢 TheMadTechnician。您的回复非常接近,足以让我找到答案:
$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
$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"
我已经使用 get-content 从大型文本文件中抓取特定文本。现在我需要向它添加 headers 并转换为 CSV,以便我可以使用它。所有内容都导出到没有 headers 的第 1 列,或者我得到以下内容:
#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
作为数组的 $Line 变量:
$Line.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
这是 $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
感谢 TheMadTechnician。您的回复非常接近,足以让我找到答案:
$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