locked
Outputting to Text File RRS feed

  • Question

  • Help for a newbie?

    Trying to analyse some robocopy log files for errors, and have got a powershell script which outputs to the screen. (this isn't a robocopy question)

    Can't work out where in the script to put the Out-File, and what the syntax should be.

    I have tried | Out-File -FilePath c:\data\test.txt 

    And whilst I get the text file. it's empty and the output is still on the screen.

    My code is as follows, without the out-file cmd and I really would appreciate advise, as this is my first dabble into powershell.

    Thanks

    $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  
        }  


    Thursday, January 4, 2018 11:05 AM

Answers

  • Thanks 

    using the start and stop transcript has done the trick

    Thursday, January 4, 2018 1:39 PM

All replies

  • RC has a switch that defines the logging options and output file:

    RoboCopy /?

    This is not a RoboCopy forum.  Please post RC issues to the Windows forum for you OS.

    Look in Gallery for scripts that parse RC logs.

    Script Gallery.


    \_(ツ)_/

    Thursday, January 4, 2018 11:12 AM
  • Thanks for the reply, however this isn't relating to robocopy, but to powershell and how I can get what comes up in the console to write to a text file. 
    Thursday, January 4, 2018 11:25 AM
  • 'hello world' | Out-File filename.ext -Append

    \_(ツ)_/

    Thursday, January 4, 2018 11:41 AM
  • Thanks 

    using the start and stop transcript has done the trick

    Thursday, January 4, 2018 1:39 PM