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.

2 comments:

  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: http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/29/learn-how-to-use-switch-regex-and-powershell-to-parse-files.aspx )
    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

    ReplyDelete
  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

    Thanks

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...