I've created a script to parse logs into a form that's simpler to work with / sharing here for others with similar requirements to the OP.
For the specific question, here's how this code would be used:
[string[]]$logs = @('C:\Scripts\testing\SSIS\testfiles\fzs-2014-04-15.log') # you could add more / get these via Get-ChildItem / etc
[PSCustomObject[]]$data = $logs |
Get-FzLogData |
ForEach-Object {
if($_.Msg -match '\s*STOR\s(?<Filename>.+)') {
[PSCustomObject]@{
FileName = $Matches['FileName']
DateTime = $_.DateTime
}
}
}
$data # just shows the data for now...
Full Code:
class FzLogEntry {
[long]$SessionId
[DateTime]$DateTime
[string]$User
[version]$ClientIp # I've used version assuming it's always IPv4... if that assumption's wrong we may have to amend to string
[string]$Msg
FzLogEntry(){}
}
Function Get-FzLogData {
[OutputType('FzLogEntry[]')]
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string[]]$Path
)
Process {
foreach ($p in $Path) {
Get-Content -Path $p | ConvertTo-FzLogEntry
}
}
}
Function ConvertTo-FzLogEntry {
[OutputType('FzLogEntry')]
[CmdletBinding(DefaultParameterSetName = 'Default')]
Param (
[Parameter(ParameterSetName = 'Default', Mandatory = $true, ValueFromPipeline = $true)]
[Parameter(ParameterSetName = 'Unparsable', Mandatory = $true, ValueFromPipeline = $true)]
[string[]]$Line
,
[Parameter(ParameterSetName = 'Unparsable', Mandatory = $true)] # I don't really have a need for this; but it may be helpful if debugging to know when lines are not processed / have a gap in your results...
[Switch]$IncludDefaultForUnparsable
,
[Parameter(ParameterSetName = 'Unparsable')]
[FzLogEntry]$DefaultValue = $null
)
Begin {
[string]$regexPattern = @'
^\(
(?<SessionId>\d+)
\)\s
(?<DateTime>\S+\s\S+)
\s\-\s
(?<User>(?:\([^\)]+\))|(?:\S+))
\s\(
(?<ClientIp>[^>]+)
\)>\s*
(?<Msg>.*)
$
'@ -replace '[\r\n]+', ''
}
Process {
foreach ($l in $Line) {
if ($l -match $regexPattern) {
$fiddle = $Matches
$fiddle.Remove(0)
$fiddle.DateTime = $fiddle.DateTime -replace '^(\d+)\/(\d+)\/(\d+)', '$3-$2-$1' #correct date format to something that PS can auto convert to a DateTime. Assumes dd/mm/yyyy format in the logs
[FzLogEntry]$fiddle
} else {
if ($IncludDefaultForUnparsable.IsPresent) {
$DefaultValue
}
}
}
}
}
Related Gist will be kept up to date should I make any changes to this function.