none
Robocopy in Powershell RRS feed

  • Question

  • I am trying to use robocopy which monitors the changes every 10 minutes using /MOT option in powerhsell to copy the source to multiple destinations and would like to use powershell to trap the error if robocopy fails to copy to any of its destination. How can i implement it?.
    satyam
    Monday, December 20, 2010 9:17 PM

Answers

  • Sorry, need to re-arrange that a little:

    $rc_job = start-job -scriptblock {robocopy c:\testfiles c:\testfiles2 /S /MOT:10}
    while ($true){

    $events = receive-job $rc_job

    $events

    start-sleep -seconds 600

    }



    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, December 20, 2010 11:07 PM

All replies

  • I'd look at using background jobs for that.  Run your robocopy using start-job, then periodically run receive-job with a process/sleep loop, and parse that for whatever text indicates a copy failure occured.

     

    Disclaimer - This is a total WAG - I've never done, or tested it.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, December 20, 2010 9:44 PM
  • Can i have a sample script if possible.

    satyam
    Monday, December 20, 2010 9:59 PM
  • This is a sample of an endless loop that will start the copy job, and check the output every 10 minutes and display it to the console.

    You'll need to parse $events for text that indicates a copy failure occurred.

    $rc_job = start-job -scriptblock {robocopy c:\testfiles c:\testfiles2 /S /MOT:10}
    while ($true){

    $events = receive-job $rc_job
    start-sleep -seconds 600

    $events
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, December 20, 2010 10:58 PM
  • Sorry, need to re-arrange that a little:

    $rc_job = start-job -scriptblock {robocopy c:\testfiles c:\testfiles2 /S /MOT:10}
    while ($true){

    $events = receive-job $rc_job

    $events

    start-sleep -seconds 600

    }



    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Monday, December 20, 2010 11:07 PM
  • what does start-sleep -seconds 600 does? Its already mentioned in the robocopy to monitor for 10 minutes.

    How can we use $events to trap the error and parse it?.

     


    satyam
    Monday, December 20, 2010 11:50 PM
  • You're actually running two processes there, the powershell script, and robocopy. 

     The start-sleep causes the main script to go to sleep, and wake up every 10 minutes to see if the Robocopy job has anything to report.  Since the robocopy job is going to wait 10 minutes to see if there are any more changes, there's no point in checking for results before then. The start-sleep should put the main script on the same schedule, and just behind the robocopy so you don't waste cpu checking for results before they're ready.

    $events will be the text output of the robocopy job, and you would parse it just as you would any other text data.  The best method will depend on what the data looks like.  I'm afraid I can't get more specific without a sample of the text you wnat to parse, and what it is you want out of it.

    Like I said earlier, this is not something I've done before, or tested.  This is admittedly a little outside the box, and someone else with more experice or a better idea may come along with better stuff.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Tuesday, December 21, 2010 12:04 AM