Author Posts

January 13, 2016 at 7:59 am

Hi!

I'm trying to make a Bits-Transfer script using a progress bar... Found another thread here on the forums but I need to check another thing to finish it...
When I'm using the code below, it works fine, but If I want to remove the "pause" line... it doesn't works anymore... Anyone knows why? I want just to start the transfer, show progress and then complete the transfer to convert the temp file on destination.


Import-Module BitsTransfer

$Source = Read-Host "Type source path"
$Destination = Read-Host "Type destination path"

Start-BitsTransfer -DisplayName "File copy" -Source $Source -Destination $Destination -Asynchronous

$Bits = Get-BitsTransfer | where {$_.DisplayName -eq "File copy"}

While ($Bits.JobState -eq "Transferring" -or $Bits.JobState -eq "TransientError" -or $Bits.JobState -eq "Connecting" -or $Bits.JobState -eq "Error" -and $pct -ne 100){
if ($Bits.JobState -eq "Error"){
Resume-BitsTransfer -BitsJob $Bits
}
$pct = [int](($Bits.BytesTransferred*100) / $Bits.BytesTotal)
Write-Progress -Activity "Copying file..." -CurrentOperation "$pct% complete"
}
Write-Warning "Copy terminated!"
Write-Host
pause
Write-Host
$Bits | Complete-BitsTransfer

January 13, 2016 at 8:25 am

this is all I use
function BitsDownload {

[CmdletBinding()]
param (
[Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [System.String] $DestinationPath,
[Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [System.String] $Uri
)
$destinationFilename = [System.IO.Path]::GetFileName($DestinationPath)
$startBitsTransferParams = @{
Source = $Uri;
Destination = $DestinationPath;
TransferType = 'Download'
DisplayName = "Downloading $destinationFilename"
Description = $Uri
Priority = 'Foreground'
}
Start-BitsTransfer @startBitsTransferParams #-ErrorAction Stop

} #end function SetBitsDownload

January 13, 2016 at 8:29 am

David Jones,

Thanks for replying... but I forgot to mention that it is an asynchronus copy...
In that way Bits-Transfer create a temp file in the destination and there is no progress bar by default.

January 13, 2016 at 8:52 am

In this way is working:


Import-Module BitsTransfer

$Source = Read-Host "Type source path"
$Destination = Read-Host "Type destination path"

$Job = Start-BitsTransfer -Source $Source -Destination $Destination -Asynchronous

While( ($Job.JobState.ToString() -eq 'Transferring') -or ($Job.JobState.ToString() -eq 'Connecting') )
{
$pct = [int](($Job.BytesTransferred*100) / $Job.BytesTotal)
Write-Progress -Activity "Copying file..." -CurrentOperation "$pct% complete"
}
Complete-BitsTransfer -BitsJob $Job

Thanks!