BitsTransfer,百分比输出

时间:2017-11-15 11:25:00

标签: powershell

我想以百分比显示下载状态。

我使用bitjob完成任务。

  

方法调用失败,因为[System.Object []]不包含   名为'op_Division'的方法。       在C:\ Users \ administrator \ Desktop \ test.ps1:56 char:8       + $ per = $ CopyMyBackups.BytesTransferred / $ CopyMyBackups.BytesTotal ...       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~           + CategoryInfo:InvalidOperation:(op_Division:String)[],RuntimeException           + FullyQualifiedErrorId:MethodNotFound         您无法在空值表达式上调用方法。       在C:\ Users \ administrator \ Desktop \ test.ps1:57 char:8       + Write-Progress -activity“Copying”-status($ per.ToString()+“%...       + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~           + CategoryInfo:InvalidOperation:(:) [],RuntimeException           + FullyQualifiedErrorId:InvokeMethodOnNull

我的脚本如下:

 }

$BitJobs=Import-Csv "$ScriptPath\$CSVListName" | Start-BitsTransfer -Asynchronous -Priority Normal -DisplayName CopyMyBackups

    while ($CopyMyBackups | ?{ $_.JobState -ne "Transferred"})
    {
       #  this
       $per=$CopyMyBackups.BytesTransferred/$CopyMyBackups.BytesTotal*100
       Write-Progress -activity "Copying" -status ($per.ToString()+"%") -PercentComplete $per

      #$CopyMyBackups = Get-BitsTransfer| ?{ $_.DisplayName -eq "CopyMyBackups"}

       Start-Sleep 10
}  }
$CopyMyBackups| Complete-BitsTransfer

1 个答案:

答案 0 :(得分:0)

我的猜测是你从CSV开始几个作业,这意味着当你试图获得BytesTransferred属性时,PowerShell会返回一个数组([System.Object[]])。

您需要使用

之类的内容迭代$CopyMyBackups或获得总数BytesTransferred/BytesTotal
($CopyMyBackups.BytesTransferred | Measure-Object -Sum).Sum