[英]Powershell limiting BITS transfers to 4 at a time
我正在使用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--
}
}
使用for
-loop,您將每次運行將$JobsCount
重置為0
for ($JobsCount = 0; $JobsCount -le 4; $JobsCount++)
像這樣的事情應該起作用(只需對當前代碼進行最少的更改):
$JobsCount = 0
While (Get-BitsTransfer | Where JobState -EQ "Suspended")
{
Get-BitsTransfer | Where JobState -EQ "Suspended" | ForEach-Object {
# remove for-loop
if ($JobsCount -lt 4)
{
$JobsCount++
...
您還必須修改while
-loop,當前,當不再有JobState
"Suspended"
作業時,它將退出,但是您仍然必須等待JobState
"Transferring"
作業,並在它們為"Transferred"
時完成。 建議: While ((Get-BitsTransfer) -ne $null)
或更簡單的While (Get-BitsTransfer)
感謝您的建議,達到了目的。 我計划進一步完善代碼,但是到目前為止,這是我目前可以使用的。
function Update-JobProgress
{
[CmdletBinding()]
param ()
Get-BitsTransfer | Where-Object { ($_.JobState -EQ "Transferring") -and ($_.DisplayName -Like "*File Transfer") } | 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))
}
}
While (Get-BitsTransfer)
{
$JobsCount = (Get-BitsTransfer | Where-Object { ($_.JobState -ne "Suspended") -and ($_.DisplayName -Like "*File Transfer") }).Count
Get-BitsTransfer | Where-Object JobState -EQ "Suspended" | ForEach-Object {
if ($JobsCount -lt 4)
{
Resume-BitsTransfer -BitsJob $_ -Asynchronous
$JobsCount++
}
}
Update-JobProgress
if (Get-BitsTransfer | Where-Object { ($_.JobState -EQ "Transferred") -and ($_.DisplayName -Like "*File Transfer") })
{
Get-BitsTransfer | Where-Object { ($_.JobState -EQ "Transferred") -and ($_.DisplayName -Like "*File Transfer") } | ForEach-Object {
Write-Progress `
-Id $([math]::Abs($_.DisplayName.GetHashCode())) `
-Activity "$($_.DisplayName)" `
-Completed
$_ | Complete-BitsTransfer
$JobsCount--
}
}
}
在測試中,我發現使用-PercentComplete開關在控制台窗口中實際顯示滾動進度條時,結果不一致。 完成的第一份工作往往會流連忘返,並引起奇怪的視覺故障。 省略開關並放棄進度條似乎可以解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.