0

I know some PowerShell, but I am not a master at PowerShell. I have a question about the code listed in the answer for Custom RoboCopy Progress Bar in PowerShell

My follow-up question is: How can this script be adapted for to run several consecutive robocopy commands?

I need to mirror files from different drives with different sources and different destinations to my portable USB3 Drive.

Thank you for any help.

Kevin

Community
  • 1
  • 1
  • Well, since the code you mention that does the copy (`Copy-File`) is a function, you can call it easily and repeatedly for different groups of files. What have you tried? – Graham Gold Jan 18 '14 at 18:38

1 Answers1

1

You can use PowerShell Jobs to run multiple robocopy jobs simultaneously. For more information, run:

Get-Help -Name about_Jobs;

Alternatively, you can simply call Start-Process several times, and omit the -Wait parameter.

Get-Help -Name Start-Process -Full;

Here is an example of how to kick off several PowerShell Background Jobs, and return status from all of them:

# Define a ScriptBlock that does some "work"
# NOTE: ScriptBlock should output an integer that indicates percentage complete
$ScriptBlock = {
    1..100 | % { $_; Start-Sleep -Milliseconds (Get-Random -Minimum 5 -Maximum 200); };
    }
# Kick off several jobs (with unique names)
1..3 | % { Start-Job -ScriptBlock $ScriptBlock -Name ('Complex Job {0}' -f $_); };

# Display Progress Bars until all jobs are completed
while (($JobList = Get-Job -State Running)) {
    foreach ($Job in $JobList) {
        try {
            # Get the most recent status
            $Percent = (Receive-Job -Job $Job -Keep)[-1];
            Write-Progress -Activity 'Background Jobs' -CurrentOperation $Job.Name -Id $Job.Id -PercentComplete $Percent;
        }
        catch { Write-Verbose -Message ('Couldn''t get percentage completed from: {0}' -f $Job.Id); }
    }
    Start-Sleep -Milliseconds 200;
}

PowerShell Progress Bars

  • He wants progress bars for how far through `robocopy` is, not just % through current file but progress through all files to be copied. – Graham Gold Jan 18 '14 at 19:04
  • If I create a variable with the total number of files and run all the RoboCopy jobs at the same time, will this give me a progress bar of the % complete for all jobs? – Kevin Klingerman Jan 18 '14 at 21:27
  • I posted a new answer on the other question, that yours is a follow-up to. The answer contains a script that properly wraps the robocopy command, including a PowerShell Progress Bar. Here is a video demonstrating it: https://www.youtube.com/watch?v=z9KeYa842rc. –  Jan 18 '14 at 21:29
  • 1
    @KevinKlingerman: I just updated my answer to demonstrate how to kick off multiple PowerShell Background Jobs, and get status from all of them. If you combine this technique with my robocopy technique from the other question, you should be able to achieve some cool results. –  Jan 18 '14 at 21:48
  • Thanks, @GrahamGold! You were right though, my first, simplistic answer didn't really do it justice. I put some more time / research into it, and got something that was workable. –  Jan 18 '14 at 22:12
  • Thank you I'll give it shot in the morning. Best Wishes. -Kevin – Kevin Klingerman Jan 18 '14 at 23:53
  • OK. I see this line in the script" "1..3 | % { Start-Job -ScriptBlock $ScriptBlock -Name ('Complex Job {0}' -f $_); };". I am not sure what the 1..3 or the 'Complex Job' section are. Where do I put my Robocopy commands? – Kevin Klingerman Jan 19 '14 at 02:03
  • All that is doing is kicking off three (3) PowerShell Background Jobs, and giving each of them a unique name (eg. "Complex Job 1"). The contents of the `$ScriptBlock` variable is what contains the code that is executing inside of the PowerShell job. –  Jan 19 '14 at 02:06
  • OK. In what portion of the $Scriptblock" code do I add my robocopy command and how do I define the additional jobs? Do I repeat the "$scriptblock" code with a different variable and then repeat the "StartJob" code with the additional variable? – Kevin Klingerman Jan 19 '14 at 18:22
  • Kevin, you'd have to variable-ize the `$ScriptBlock`, so that it can accept arguments. For example: `Start-Job -ScriptBlock { $args[0]; } -ArgumentList 1;` –  Jan 20 '14 at 18:26