Powershell: Analyse Robocopy Logs for Error´s with Regex
Ein kleines Script welches Robocopy Log-Daten nach Fehlern durchsucht und konsolidiert.
ein Auszug eines Fehlers:
2014/07/07 15:39:31 ERROR 123 (0x0000007B) Creating Destination Directory \\mswfil\USR$\aabbcc\””\
Mittels RegExr (an online tool to learn, build, & test Regular Expressions) habe ich folgendes generiert: ([A-Z]{3,}).([0-9]{3,}).([(])+([0-9])+([x])+([0-9]{3,}).([)]).([A-Za-z]{3,}).([A-Za-z]{3,}). und nun Powershell
#Path to the Log Folder, Logfiles has to be txt pathtologs = "C:\Robocopy\Logs" #Path to the ReportFolder $exportpath = "C:\Robocopy\Report" $script:myarray = @() function analyse($file) { $regex = "([A-Z]{3,}).([0-9]{3,}).([(])+([0-9])+([x])+([0-9]{3,}).([)]).([A-Za-z]{3,}).([A-Za-z]{3,})." $input = Select-String -Path $file -Pattern $regex -AllMatches if ($input.Matches -notlike "") { foreach ($line in $input) { $temp = ($line.Line).split(" ") $script:myarray += New-Object psobject -Property @{ Date = $temp[0] Time = $temp[1] ErrorCode=$temp[4].Replace("(","").Replace(")","") Path=[regex]::split($line.Line, '([A-Z]{3,}).([0-9]{3,}).([(])+([0-9])+([x])+([0-9]{3,}).([)]).([A-Za-z]{3,}).([A-Za-z]{3,}).')[10] } } } $input = $null Get-ChildItem $pathtologs -Recurse -Filter "*.txt" -Exclude "Errors.txt"| foreach {$_.Fullname analyse $_.Fullname} $script:myarray | Export-Csv "$($exportpath)\report.csv"