Thursday, April 21, 2011

Analyse Robocopy logs with Powershell

I have often had to analyse many robocopy logs and wasted a lot of time running through huge text files. That's why I wanted to share here a script I coded to run through logfiles generated by robocopy.exe and print out just a summary of useful information. The logfiles to analyse are generated using the following robocopy switch:

/LOG+:C:\log_robocopy\log_robocopy_01.txt

Here's the script. It takes in all the robocopy logfiles and recursively prints out statistics about failed file copies. The output report can be easily read.
  1. $files=get-childitem \\servername\c$\log_robocopy\*.txt  
  2. $pattern1 = $null  
  3. $pattern2 = $null  
  4. $pattern3 = $null  
  5. $pattern4 = $null  
  6. foreach ($file in $files)  
  7.     {  
  8.     write-host "Working on" $file  
  9.     select-string $file -pattern " Started : "  
  10.     select-string $file -pattern "Source : "  
  11.     select-string $file -pattern "Dest : "  
  12.     $pattern1 = select-string $file -pattern "Files :  "  
  13.     $pattern1 = $pattern1.tostring() -replace '\s+'" "  
  14.     $pattern2 = $pattern1.tostring().split(" ")  
  15.     write-host "Total files on source:`t" $pattern2[3]  
  16.     write-host "Total files copied:`t`t" $pattern2[4]  
  17.     write-host "Total files skipped:`t" $pattern2[5]  
  18.     write-host "Total files failed:`t`t" $pattern2[7]  
  19.     $pattern3 = select-string $file -pattern "Bytes : "  
  20.     $pattern3 = $pattern3.tostring() -replace " 0 "" 0 m "  
  21.     $pattern3 = $pattern3.tostring() -replace '\s+'" "  
  22.     $pattern4 = $pattern3.tostring().split(" ")  
  23.     write-host "Total bytes on source:`t" $pattern4[3] $pattern4[4]  
  24.     write-host "Total bytes copied:`t`t" $pattern4[5] $pattern4[6]  
  25.     write-host "Total bytes skipped:`t" $pattern4[7] $pattern4[8]  
  26.     write-host "Total bytes failed:`t`t" $pattern4[11] $pattern4[12]  
  27.     $error1 = select-string $file -pattern "0x00000002"  
  28.     write-host "File not found error :"$error1.count  
  29.     $error2 = select-string $file -pattern "0x00000003"  
  30.     write-host "File not found errors :"$error2.count  
  31.     $error3 = select-string $file -pattern "0x00000005"  
  32.     write-host "Access denied errors :"$error3.count  
  33.     $error4 = select-string $file -pattern "0x00000006"  
  34.     write-host "Invalid handle errors :"$error4.count  
  35.     $error5 = select-string $file -pattern "0x00000020"  
  36.     write-host "File locked errors :"$error5.count  
  37.     $error6 = select-string $file -pattern "0x00000035"  
  38.     write-host "Network path not found errors :"$error6.count  
  39.     $error7 = select-string $file -pattern "0x00000040"  
  40.     write-host "Network name unavailable errors :"$error7.count              
  41.     $error8 = select-string $file -pattern "0x00000070"  
  42.     write-host "Disk full errors :"$error8.count  
  43.     $error9 = select-string $file -pattern "0x00000079"  
  44.     write-host "Semaphore timeout errors :"$error9.count  
  45.     $error10 = select-string $file -pattern "0x00000033"  
  46.     write-host "Network path errors :"$error10.count  
  47.     $error11 = select-string $file -pattern "0x0000003a"  
  48.     write-host "NTFS security errors :"$error11.count         
  49.     $error12 = select-string $file -pattern "0x0000054f"  
  50.     write-host "Internal errors :"$error12.count  
  51.     select-string $file -pattern "Ended : "  
  52.     write-host "============================="  
  53.     sleep 2  
  54.     }  

I hope this helps. For more information about robocopy error codes have a look here.

Please leave a comment if this script was useful top you or if you would like to suggest an improvement.

Related Posts Plugin for WordPress, Blogger...