Я хочу преобразовать файл журнала IIS (в формате W3C) в файл CSV или XLS в PowerShell или C#.
Я пытаюсь использовать этот код в PowerShell:
$LogFolder = "C:\iislog\"
$LogFiles = [System.IO.Directory]::GetFiles($LogFolder, "*.log")
$LogTemp = "C:\iislog\end.csv"
# Logs will store each line of the log files in an array
$Logs = @()
# Skip the comment lines
$LogFiles | % { Get-Content $_ | where {$_ -notLike "#[D,F,S,V]*" } | % { $Logs += $_ } }
# Then grab the first header line, and adjust its format for later
$LogColumns = ( $LogFiles | select -first 6 | % { Get-Content $_ | where {$_ -Like "#[F]*" } } ) `
-replace "#Fields: ", "" -replace "-","" -replace "\(","" -replace "\)",""
# Temporarily, store the reformatted logs
Set-Content -LiteralPath $LogTemp -Value ( [System.String]::Format("{0}{1}{2}", $LogColumns, [Environment]::NewLine, ( [System.String]::Join( [Environment]::NewLine, $Logs) ) ) )
# Read the reformatted logs as a CSV file
$Logs = Import-Csv -Path $LogTemp -Delimiter " "
# Sample query : Select all unique users
$Logs | select -Unique csusername
Но этот код не разделяет столбцы и печатает каждую строку в один столбец в CSV (при открытии end.csv с помощью excel).
Как я могу решить эту проблему?
Я хочу, чтобы столбцы отделялись друг от друга в выходном файле.