Bits-Transfer with progress bar

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Vandrey Trindade Vandrey Trindade 8 months, 2 weeks ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #33841
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    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

    #33843
    Profile photo of David Jones
    David Jones
    Participant

    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

    #33844
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    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.

    #33846
    Profile photo of Vandrey Trindade
    Vandrey Trindade
    Participant

    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!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.