Powershell Robocopy Log-Aufbereitung für Excel

Posted on: September 8, 2014 Posted by: Philipp Comments: 0

Powershell Robocopy Log-Aufbereitung für Excel

Mit diesem kleinen Script war es möglich 6000 Log-Files welche durch einzelne Robocopy-Kopiervorgänge erzeugt wurden auszuwerten.

Nach dem Prinzip “short&simple” setzt dieses Skript lediglich auf String replacement, sowie das Splitten von Variablen.

OS:Windows2012R2

#Path to the Log Folder, Logfiles has to be txt
 $pathtologs = "C:\Robocopy\Logs"
 #Path to the ReportFolder
 $exportpath = "C:\Robocopy\Report"

function main{
 #get all log-files recurse and analyse every file
 Get-ChildItem $pathtologs -Recurse -Filter "*.txt" -Exclude "Erro*.txt" | foreach {
 Write-host "Processing $($_.Fullname)"
 analyse $_.Fullname
 }

$script:myarray = @()

function analyse($file)
 {
 $input = Select-String -Path $file -Pattern "Robust File Copy for Windows " -AllMatches
 if ($input.Matches -notlike "")
 {

$regexstart = '(Started : ?)'

$Content = Get-Content $input.Path
 $Fileinhalt=$Dirs=$Started = $Source = $Dest = $Ended = $Speed = $i=$NULL
 $i = 0
 foreach ($line in $Content){
 $i++
 if ($line -like "*Started :*"){$Started = $line.replace(" Started : ","")}
 if ($line -like "*Source =*"){$Source = $line.replace(" Source = ","")}
 if ($line -like "*Dest :*"){$Dest = $line.replace(" Dest : ","")}
 if ($line -like "*Ended :*"){$Ended = $line.replace(" Ended : ","")}
 if ($line -like "*Speed :*MegaBytes*"){$Speed = $line.replace("Speed :","").replace(" ","").replace("MegaBytes/min.","")}
 if ($line -like "*Ended :*"){$Ended = $line.replace(" Ended : ","")}
 if ($line -like " Dirs :*"){$Dirs = $line.replace(" Dirs : ","").replace(" ",".").replace("..",".").replace("..",".").replace("..",".").replace("..",".")}
 if ($i -ge 12 -and $line -like "*Files :*"){$Fileinhalt = $line.replace(" Files :","").replace(" ",".").replace("..",".").replace("..",".").replace("..",".").replace("..",".")}
 if ($line -like "*Bytes :*"){$Bytes = $line.replace(" Bytes :","").replace(" ",";").replace(";;",";").replace(";;",";").replace(";;",";").replace(";;",";").replace(";m"," m").replace(";g"," g").replace(";k"," k")}
 if ($line -like " Times :*"){$Time = $line.replace(" Times :","").replace(" ",";").replace(";;",";").replace(";;",";").replace(";;",";").replace(";;",";")}
 if ($line -like " Ended : *"){$Ended = $line.replace(" Ended : ","")}

if ($Dirs){
 $Dirs= $Dirs.split(".")
 $Dirs_Total = $Dirs[1]
 $Dirs_Copied =$Dirs[2]
 $Dirs_Skipped = $Dirs[3]
 $Dirs_Mismatch =$Dirs[4]
 $Dirs_FAILED =$Dirs[5]
 $Dirs_Extras = $Dirs[6]
 }
 if ($Fileinhalt)
 {
 $Files= $Fileinhalt.split(".")
 $Files_Total = $Files[1]
 $Files_Copied =$Files[2]
 $Files_Skipped = $Files[3]
 $Files_Mismatch =$Files[4]
 $Files_FAILED =$Files[5]
 $Files_Extras = $Files[6]

}
 if ($Bytes)
 {
 $Bytes= $Bytes.split(";")
 $Bytes_Total = $Bytes[1]
 $Bytes_Copied =$Bytes[2]
 $Bytes_Skipped = $Bytes[3]
 $Bytes_Mismatch =$Bytes[4]
 $Bytes_FAILED =$Bytes[5]
 $Bytes_Extras = $Bytes[6]
 }

if ($Time)
 {
 $Time= $Time.split(";")
 $Time_Total = $Time[1]
 $Time_Copied =$Time[2]

}
 }

$script:myarray += New-Object psobject -Property @{
 Started = $Started
 Source = $Source
 Destination = $Dest
 Ended=$Ended
 Speed=$Speed
 Dirs_Total = $Dirs_Total
 Dirs_Copied = $Dirs_Copied
 Dirs_Skipped = $Dirs_Skipped
 Dirs_Mismatch=$Dirs_Mismatch
 Dirs_FAILED=$Dirs_FAILED
 Dirs_Extras=$Dirs_Extras
 Files_Total = $Files_Total
 Files_Copied = $Files_Copied
 Files_Skipped = $Files_Skipped
 Files_Mismatch = $Files_Mismatch
 Files_FAILED = $Files_FAILED
 Files_Extra = $Files_Extras
 Bytes_Total = $Bytes_Total
 Bytes_Copied = $Bytes_Copied
 Bytes_Skipped = $Bytes_Skipped
 Bytes_Mismatch = $Bytes_Mismatch
 Bytes_FAILED = $Bytes_FAILED
 Bytes_Extra = $Bytes_Extras
 Time_Total = $Time_Total
 Time_Copied = $Time_Copied

}
 $script:myarray | export-csv "$($exportpath)\overall_$((get-date).ToShortDateString().replace("/","_")).csv"
 }
 }
 main
 }