Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
$paths =
"C:\SyncLog\logFile1.log",
"C:\SyncLog\logFile2.log",
"C:\SyncLog\logFile3.log",
"C:\SyncLog\logFile4.log",
"C:\SyncLog\logFile5.log"
$reportsPath = "C:\SyncLog\Reports"
foreach ($p in $paths) {
$end = select-string -Path $p -Pattern "Ended\s+:"
$start = select-string -Path $p -pattern 'Started\s+:'
$table = ($start + $end) | sort LineNumber | select -expa line | % {$_.trim()}
$ret = @()
for ($i=0; $i -le $table.Count; $i++){
if($table[$i] -like "Started*"){
if ($table[$i+1] -like "Ended*"){
$s = [datetime]($table[$i] -replace "Started : ", "")
$e = [datetime]($table[$i+1] -replace "Ended : ", "")
$ret += [pscustomobject]@{Start=$s; End=$e; Duration = ($e - $s); Status = 'Success'}
}
else {
$s = [datetime]($table[$i] -replace "Started : ", "")
if ($i -lt $table.Count-1) {
$e = [datetime]($table[$i+1] -replace "Started : ", "")
$ret += [pscustomobject]@{Start=$s; End=$null; Duration = ($e - $s); Status = "Failed"}
}
else{
$s = [datetime]($table[$i] -replace "Started : ", "")
$ret += [pscustomobject]@{Start=$s; End=$null; Duration = $null; Status = "Running"}
}
}
}
}
$fileName = [io.path]::GetFileName($p)
$ext = [io.path]::GetExtension($p)
$outFile = "$($fileName -replace $ext,'').csv"
$ret | export-csv -Path (Join-Path $reportsPath $outFile) -NoTypeInformation -Force -Delimiter ';'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment