2023-04-06 18:24:09 +00:00
|
|
|
param (
|
|
|
|
[Parameter(Mandatory = $false)][string] $inFile,
|
|
|
|
[Parameter(Mandatory = $false)][string] $outFile,
|
|
|
|
[Parameter(Mandatory = $false)][int] $lines = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
if ([string]::IsNullOrWhiteSpace($inFile)) {
|
|
|
|
$inFile = [System.Environment]::GetEnvironmentVariable("INFILE")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([string]::IsNullOrWhiteSpace($outFile)) {
|
|
|
|
$outFile = [System.Environment]::GetEnvironmentVariable("OUTFILE")
|
|
|
|
}
|
|
|
|
|
|
|
|
$baseDate = (New-Object System.DateTime -ArgumentList @(1970, 1, 1, 0, 0, 0, [DateTimeKind]::Utc)).ToLocalTime()
|
|
|
|
$builder = New-Object System.Text.StringBuilder
|
|
|
|
|
|
|
|
$outputStream = [IO.File]::Open($outFile, [IO.FileMode]::Append, [IO.FileAccess]::Write, [IO.FileShare]::Read)
|
|
|
|
$outputWriter = New-Object -TypeName System.IO.StreamWriter -ArgumentList @($outputStream, [Text.Encoding]::UTF8)
|
|
|
|
|
|
|
|
Get-Content -Tail $lines -Wait -Path $inFile | ForEach-Object {
|
|
|
|
$l = ConvertFrom-Json -InputObject $_
|
|
|
|
$r = $l.request;
|
2023-04-06 19:03:16 +00:00
|
|
|
|
|
|
|
$userAgent = $r.headers.'User-Agent' | Select-Object -First 1
|
|
|
|
$referer = $r.headers.Referer | Select-Object -First 1
|
2023-04-06 18:24:09 +00:00
|
|
|
|
|
|
|
$null = $builder.Clear()
|
|
|
|
$null = $builder.Append($r.remote_ip)
|
|
|
|
$null = $builder.Append(" - [")
|
|
|
|
$null = $builder.Append($baseDate.AddSeconds($l.ts).ToString("yyyy-MM-dd HH:mm:ss.fff"))
|
|
|
|
$null = $builder.Append("] """)
|
|
|
|
$null = $builder.Append($r.host)
|
|
|
|
$null = $builder.Append(""" ")
|
|
|
|
$null = $builder.Append($r.method)
|
|
|
|
$null = $builder.Append(" ")
|
|
|
|
$null = $builder.Append($r.uri)
|
|
|
|
$null = $builder.Append(" ")
|
|
|
|
$null = $builder.Append($r.proto)
|
|
|
|
$null = $builder.Append(" ")
|
|
|
|
$null = $builder.Append($l.status)
|
|
|
|
$null = $builder.Append(" ")
|
|
|
|
$null = $builder.Append($l.size)
|
|
|
|
$null = $builder.Append(" """)
|
2023-04-06 19:03:16 +00:00
|
|
|
$null = $builder.Append($referer)
|
2023-04-06 18:24:09 +00:00
|
|
|
$null = $builder.Append(""" """)
|
2023-04-06 19:03:16 +00:00
|
|
|
$null = $builder.Append($userAgent)
|
2023-04-06 18:24:09 +00:00
|
|
|
$null = $builder.Append("""")
|
|
|
|
|
|
|
|
$commonLog = $builder.ToString()
|
|
|
|
|
|
|
|
Write-Output $commonLog
|
|
|
|
$outputWriter.WriteLine($commonLog)
|
|
|
|
$outputWriter.Flush()
|
|
|
|
|
|
|
|
#[system.string]::Join(
|
|
|
|
# " ",
|
|
|
|
# @(
|
|
|
|
# "$($r.remote_ip) - [$($baseDate.AddSeconds($l.ts).ToString("yyyy-MM-dd HH:mm:ss.fff"))]",
|
|
|
|
# """$($r.host)""",
|
|
|
|
# $r.method,
|
|
|
|
# $r.uri,
|
|
|
|
# $r.proto,
|
|
|
|
# $l.status,
|
|
|
|
# $l.size,
|
|
|
|
# """$($r.headers.Referer)""",
|
|
|
|
# """$ua"""
|
|
|
|
# ))
|
|
|
|
}
|
|
|
|
|