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:


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.


  1. Hello Happy Sysadmin,

    it is very good idea that you use PS to parse the logs!!!

    One thing I would change:

    Instead of issuing multiple Select-String commands that have to read the file again and again, you could read it once and use a regex to parse it line by line adding up the values you want to display in the end or just remembering them for later use!
    You can use a "swtich -regex -file" command to accomplish this ( see: )
    This means: less IO by reading the file once

    You can report all the results after having read the file at the end of your script.

    kind regards,
    Klaus (Schulte) - KSchulte

  2. Hi Klaus,

    thanks for your feedback!

    I have rewritten the second part of the script but I am now noticing 4-times poorer performances, which I cannot explain for the moment.

    Maybe my syntax can be improved to solve this. Have you got any other suggestion? Please share your feedback!

    Here's the new second part of the script:

    $hash = @{}
    switch -regex (Get-Content $file)
    "0x00000002" {$count02=$count02+1}
    "0x00000003" {$count03=$count03+1}
    "0x00000005" {$count05=$count05+1}
    "0x00000006" {$count06=$count06+1}
    "0x00000020" {$count20=$count20+1}
    "0x00000035" {$count35=$count35+1}
    "0x00000040" {$count40=$count40+1}
    "0x00000070" {$count70=$count70+1}
    "0x00000079" {$count79=$count79+1}
    "0x00000033" {$count33=$count33+1}
    "0x0000003a" {$count3a=$count3a+1}
    "0x0000004f" {$count4f=$count4f+1}
    write-host "File not found error :"$count02
    write-host "File not found errors :"$count03
    write-host "Access denied errors :"$count05
    write-host "Invalid handle errors :"$count06
    write-host "File locked errors :"$count20
    write-host "Network path not found errors :"$count35
    write-host "Network name unavailable errors :"$count40
    write-host "Disk full errors :"$count70
    write-host "Semaphore timeout errors :"$count79
    write-host "Network path errors :"$count33
    write-host "NTFS security errors :"$count3a
    write-host "Internal errors :"$count4f


  3. Hi, thanks for this - it gives me what I want, with a couple of edits (too much information) - however I can't figure out how to output the results to a test file, rather than to screen. Any help would be gratefully received.

    I've tried putting the Out-File command in at various locations and just get an empty text file.

    This is my amended code.

    $files=get-childitem c:\data\logs\*.log
    $pattern1 = $null
    $pattern2 = $null
    $pattern3 = $null
    $pattern4 = $null
    foreach ($file in $files)
    write-host "Working on" $file
    $pattern1 = select-string $file -pattern "Files : "
    $pattern1 = $pattern1.tostring() -replace '\s+', " "
    $pattern2 = $pattern1.tostring().split(" ")
    write-host "Total files on source:`t" $pattern2[3]
    write-host "Total files copied:`t`t" $pattern2[4]
    write-host "Total files skipped:`t" $pattern2[5]
    write-host "Total files failed:`t`t" $pattern2[7]
    $pattern3 = select-string $file -pattern "Bytes : "
    $pattern3 = $pattern3.tostring() -replace " 0 ", " 0 m "
    $pattern3 = $pattern3.tostring() -replace '\s+', " "
    $pattern4 = $pattern3.tostring().split(" ")
    write-host "Total bytes on source:`t" $pattern4[3] $pattern4[4]
    write-host "Total bytes copied:`t`t" $pattern4[5] $pattern4[6]
    write-host "Total bytes skipped:`t" $pattern4[7] $pattern4[8]
    write-host "Total bytes failed:`t`t" $pattern4[11] $pattern4[12]
    $error1 = select-string $file -pattern "0x00000002"
    write-host "File not found error :"$error1.count
    $error2 = select-string $file -pattern "0x00000003"
    write-host "File not found errors :"$error2.count
    $error3 = select-string $file -pattern "0x00000005"
    write-host "Access denied errors :"$error3.count
    $error4 = select-string $file -pattern "0x00000006"
    write-host "Invalid handle errors :"$error4.count
    $error5 = select-string $file -pattern "0x00000020"
    write-host "File locked errors :"$error5.count
    $error6 = select-string $file -pattern "0x00000035"
    write-host "Network path not found errors :"$error6.count
    $error7 = select-string $file -pattern "0x00000040"
    write-host "Network name unavailable errors :"$error7.count
    $error8 = select-string $file -pattern "0x00000070"
    write-host "Disk full errors :"$error8.count
    $error9 = select-string $file -pattern "0x00000079"
    write-host "Semaphore timeout errors :"$error9.count
    $error10 = select-string $file -pattern "0x00000033"
    write-host "Network path errors :"$error10.count
    $error11 = select-string $file -pattern "0x0000003a"
    write-host "NTFS security errors :"$error11.count
    $error12 = select-string $file -pattern "0x0000054f"
    write-host "Internal errors :"$error12.count
    write-host "============================="
    sleep 2


Related Posts Plugin for WordPress, Blogger...