如何将 IIS W3C 日志文件转换为 CSV?
How to convert IIS W3C log file to CSV?
我想在 PowerShell 或 C# 中将 IIS 日志文件(W3C 格式)解析为 CSV 或 XLS 文件。
我在 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 中的一列(当用 excel 打开 end.csv 时)。
我该如何解决这个问题?
我希望输出文件中的列彼此分开。
我在 PowerShell 中读取这些日志的快速而肮脏的方法使用自定义函数。大多数情况下,这只是使用 ConvertFrom-CSV
和操纵 IIS 日志文件格式的前几行以满足 cmdlet 期望的问题。
function ConvertIISLogFrom-CSV{
[cmdletbinding()]
param(
[parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Alias("FullName")]
[string]$File
)
process{
Get-Content $file | Where-Object{$_ -notmatch "^#[DSV]"} | ForEach-Object{$_ -replace '^#Fields: '} | ConvertFrom-Csv -Delimiter " "
}
}
Get-ChildItem $path -Filter "ex*" |
Sort-Object creationdate -Descending |
Select -Last 1 |
ConvertIISLogFrom-CSV |
Where-Object {$_."cs-username" -eq "username" -and $_."x-fullpath" -like "*error*"} |
Select-Object date,time,"c-ip","cs-username","x-session","x-fullpath" |
Format-Table -AutoSize
该 cmdlet 将读取文件并有效删除前几行注释。我们特意保留了包含列 header 的初始过滤器中的 #fields 行。在我们摆脱#fields 之后,我们得到了正确的 CSV 格式。
使用上面的方法,您只需将 $path
更改为包含日志的位置。之后的内容主要是展示与其他 PowerShell 筛选和 cmdlet 集成的示例。
由于我们正在制作 PowerShell objects,您可以使用任何您喜欢的数据导出选项。进入 Export-CSV
就可以了。
我想在 PowerShell 或 C# 中将 IIS 日志文件(W3C 格式)解析为 CSV 或 XLS 文件。
我在 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 中的一列(当用 excel 打开 end.csv 时)。
我该如何解决这个问题?
我希望输出文件中的列彼此分开。
我在 PowerShell 中读取这些日志的快速而肮脏的方法使用自定义函数。大多数情况下,这只是使用 ConvertFrom-CSV
和操纵 IIS 日志文件格式的前几行以满足 cmdlet 期望的问题。
function ConvertIISLogFrom-CSV{
[cmdletbinding()]
param(
[parameter(ValueFromPipelineByPropertyName=$true, Mandatory=$true)]
[Alias("FullName")]
[string]$File
)
process{
Get-Content $file | Where-Object{$_ -notmatch "^#[DSV]"} | ForEach-Object{$_ -replace '^#Fields: '} | ConvertFrom-Csv -Delimiter " "
}
}
Get-ChildItem $path -Filter "ex*" |
Sort-Object creationdate -Descending |
Select -Last 1 |
ConvertIISLogFrom-CSV |
Where-Object {$_."cs-username" -eq "username" -and $_."x-fullpath" -like "*error*"} |
Select-Object date,time,"c-ip","cs-username","x-session","x-fullpath" |
Format-Table -AutoSize
该 cmdlet 将读取文件并有效删除前几行注释。我们特意保留了包含列 header 的初始过滤器中的 #fields 行。在我们摆脱#fields 之后,我们得到了正确的 CSV 格式。
使用上面的方法,您只需将 $path
更改为包含日志的位置。之后的内容主要是展示与其他 PowerShell 筛选和 cmdlet 集成的示例。
由于我们正在制作 PowerShell objects,您可以使用任何您喜欢的数据导出选项。进入 Export-CSV
就可以了。