关于“PowerShell中自定义Robocopy进度条”答案中的代码的后续任务1

时间:2014-01-18 18:27:43

标签: powershell robocopy

我知道一些PowerShell,但我不是PowerShell的高手。我对Custom RoboCopy Progress Bar in PowerShell

的答案中列出的代码有疑问

我的后续问题是:如何修改此脚本以运行多个连续的robocopy命令

我需要将来自不同来源和不同目的地的不同驱动器的文件镜像到便携式USB3驱动器。

感谢您的帮助。

凯文

1 个答案:

答案 0 :(得分:1)

您可以使用PowerShell作业同时运行多个robocopy作业。有关更多信息,请运行:

Get-Help -Name about_Jobs;

或者,您可以多次致电Start-Process,并省略-Wait参数。

Get-Help -Name Start-Process -Full;

以下是一个如何启动多个PowerShell后台作业并从所有这些作业返回状态的示例:

# 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