‘Sup PSHomies?
Got a lil’ somethin’ for ya… Get-RCLogSummary! As you know I’m a big fan of RoboCopy! I thought I’d share one of the perks of using RoboCopy: the LogFile.
Here’s a list of RoboCopy Logging options, courtesy of ss64.com
Logging options /L : List only - don’t copy, timestamp or delete any files. /NP : No Progress - don’t display % copied. /unicode : Display the status output as Unicode text. ## /LOG:file : Output status to LOG file (overwrite existing log). /UNILOG:file : Output status to Unicode Log file (overwrite) /LOG+:file : Output status to LOG file (append to existing log). /UNILOG+:file : Output status to Unicode Log file (append) /TS : Include Source file Time Stamps in the output. /FP : Include Full Pathname of files in the output. /NS : No Size - don’t log file sizes. /NC : No Class - don’t log file classes. /NFL : No File List - don’t log file names. /NDL : No Directory List - don’t log directory names. /TEE : Output to console window, as well as the log file. /NJH : No Job Header. /NJS : No Job Summary.
My preference when it comes to logging is to have seperate logfiles instead of appending to one big file. The option /NP is a no brainer, displaying ‘%’ will give you an indication how long it took for that specific file/folder, but who wants that right?It will only increase your logfile size taking more time to parse it down the line. I recently used /NDL and I must say this will keep your logfile footprint small. I did include /FP to still have an idea where the file is being copied from. I’d go with /NDL in combination with /FP when doing a delta-sync. A delta-sync is a robocopy job that will copy the differences once a full-sync has taken place. If the file hasn’t changed robocopy will skip it. Only new and newer files will be copied… Ok enough background, let get scripting shall we? 😛
Function Get-RCLogSummary{ param( [String]$LogFileName, [String[]]$LogSummary ) $objLogSummary = @{ rcLogFile = $LogFileName Speed = '' } Foreach($line in $logSummary) { switch ($line){ #Header {$_ | select-string ' Source :'} { $_= $_.ToString() $objLogSummary.Add('Source',$_.Substring(11).Trim()) } {$_ | select-string ' Dest :'} { $_= $_.ToString() $objLogSummary.Add('Target',$_.Substring(11).Trim()) } {$_ | select-string ' Started :'} { $_= $_.ToString() $objLogSummary.Add('Start',$($_.Substring(11).Trim())) } #Footer {$_ | select-string ' Dirs :'} { $_= $_.ToString() $objLogSummary.Add('TotalDirs',$_.Substring(11,10).Trim()) $objLogSummary.Add('FailedDirs',$_.Substring(51,10).Trim()) $objLogSummary.Add('CopiedDirs',$_.Substring(21,10).Trim()) } {$_ | select-string ' Files :'} { $_= $_.ToString() $objLogSummary.Add('TotalFiles',$_.Substring(11,10).Trim()) $objLogSummary.Add('FailedFiles',$_.Substring(51,10).Trim()) $objLogSummary.Add('CopiedFiles',$_.Substring(21,10).Trim()) } {$_ | select-string ' Bytes :'} { $_= $_.ToString() $objLogSummary.Add('TotalBytes',$_.Substring(11,10).Trim()) $objLogSummary.Add('FailedBytes',$_.Substring(51,10).Trim()) $objLogSummary.Add('CopiedBytes',$_.Substring(21,10).Trim()) } {$_ | select-string ' Ended :'} { $_= $_.ToString() $objLogSummary.Add('End',$($_.Substring(11).Trim())) } {$_ | select-string ' Speed :'} { $_= $_.ToString() $objLogSummary.Speed = $($_.Substring(11).Trim()) } {$_ | select-string ' Times :'} { $_= $_.ToString() $objLogSummary.Add('Time Total',$($_.Substring(11,10).Trim())) } } } #return $objLogSummary [PSCustomObject]$objLogSummary } #region:array with all LogSummary Object Properties $arrRCProperties = @( 'rcLogFile', 'Source', 'Target', 'TotalDirs', 'TotalFiles', 'TotalBytes', 'FailedDirs', 'FailedFiles', 'FailedBytes', 'CopiedDirs', 'CopiedFiles', 'CopiedBytes', 'Start', 'End', 'Time Total', 'Speed' ) #endregion #region: Get all robocopy LogFiles in specified folder and get Summary get-childitem '.\log\rc\home\22-06-2015' -File | ForEach-Object { #region: Get File Header & Footer $arrSummary = (Get-Content $_.FullName)[5..8] #Header $arrSummary += (Get-Content $_.FullName)[-11..-1] #Footer #endregion Get-RCLogSummary -LogFileName $_.Name -LogSummary $arrSummary }| Select-Object $arrRCProperties | Out-GridView #endregion
First I’ll get a list of logFiles and retrieve the first 5-8 lines and the last 10 lines of each file for processing. The LogFileName & array Summary are then passed as parameters to Get-RCLogSummary. I did a select to get the parameters in a certain order. It was a toss up between using [Ordered] Hash or defining a [PSCustomObject] beforehand. I figured you could minimize the Properties you want by tweaking the $arrRcProperties yourself. last but not least use Out-Gridview or Export-Csv to see the endresult.
I’m working on my pipeline skills, trust me my previous version was more ‘elaborate’, and by elaborate I mean over engineered…
So I guess you’ve noticed that regular expression is missing? Robocopy labels are fixed which is a good thing for me. I’m looking into it…
This regular expression isn’t as easy as it seems… This works, just don’t include /Bytes in your robocopy parameter list. In that case you’ll definitely need regular expression. Version 2.0 I guess…
Hope it’s worth something to you
Ttyl,
Urv
Pingback: RoboCopy ExitCodes the PowerShell way | pshirwin