我正在编写一个脚本,以使用 BITS 将 Office 365 ProPlus 二进制文件传输到 12 个单独的文件共享。我还想在屏幕上显示这些传输的进度。我的问题是,我想将同时 BITS 传输的数量限制为一次不超过 4 个。当一个作业完成时,我想开始队列中的下一个作业并继续接收进度,直到所有作业完成。
这就是我到目前为止所拥有的。我首先从这个函数开始,为处于挂起状态的所有网络位置创建我的所有 BITS 作业。
function Start-BinaryTransfer
{
[CmdletBinding()]
param
(
[array]$DistrictsToUpdate
)
$Source = "$BaseSource\$UpdateChannel"
if ("All" -in $DistrictsToUpdate.Split(','))
{
foreach ($Destination in $ReposList)
{
Copy-Item -Path $Source -Destination $($Destination.Location + '\') -Filter { $_.PSisContainer } -Recurse -ErrorAction SilentlyContinue
Get-ChildItem -Path $Source -Recurse | Where-Object{ $_.PSisContainer } | ForEach-Object {
$spath = $_.FullName.Remove(0, $Source.Length + 1)
$BITSJobs += Start-BitsTransfer -Source $Source\$spath\*.* `
-Destination "$($Destination.Location)\$UpdateChannel\$spath" `
-DisplayName "$($Destination.District) File Transfer" `
-Description "Transferring from [$Source] to [$($Destination.Location)\$UpdateChannel]" `
-Suspended
}
}
}
创建完所有作业后,我会尝试使用此 While 循环一次启动 4 个作业,并在执行过程中显示进度。不幸的是,实际行为是它会尝试同时启动所有 12 个作业,这会导致网络资源陷入困境。
While (Get-BitsTransfer | Where JobState -EQ "Suspended")
{
Get-BitsTransfer | Where JobState -EQ "Suspended" | ForEach-Object {
for ($JobsCount = 0; $JobsCount -le 4; $JobsCount++)
{
if ($JobsCount -lt 4)
{
Resume-BitsTransfer -BitsJob $_ -Asynchronous
Get-BitsTransfer | Where JobState -EQ "Transferring" | ForEach-Object {
Write-Progress `
-Id $([math]::Abs($_.DisplayName.GetHashCode())) `
-Activity "$($_.DisplayName)" `
-Status "$($_.Description)" `
-CurrentOperation "$([math]::Floor($_.BytesTransferred / $_.BytesTotal * 100)) % Complete" `
-PercentComplete $([math]::Floor($_.BytesTransferred / $_.BytesTotal * 100))
}
}
}
}
if (Get-BitsTransfer | Where JobState -EQ "Transferred")
{
Get-BitsTransfer | Where JobState -EQ "Transferred" | Complete- BitsTransfer
$JobsCount--
}
}