depreciated/Save-OSDProgressFile.ps1

<#
.SYNOPSIS
Download webfiles via async BitsJob and updates progressbar every 4%
 
.DESCRIPTION
Download webfiles async BitsJob and updates progressbar every 4%
 
.PARAMETER URL
WebURL of download file
 
.PARAMETER File
Parameter description
 
.EXAMPLE
An example
 
.NOTES
General notes
#>

function Save-OSDProgressFile {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$URL,

        [Parameter(Mandatory)]
        [string]$DestinationFile
    )

    Begin {

        # Check if UI is running
        ###################################################
        Stop-NoUI
    }

    Process {
        try {
            $fileName = $DestinationFile | Split-Path -Leaf
            Update-OSDProgress -DisplayBar -DownloadFile $fileName

            # Create destination folder if needed
            ###################################################
            if ($DestinationFile -match '^\.\\') {
                $DestinationFile = Join-Path (Get-Location -PSProvider "FileSystem") ($DestinationFile -Split '^\.')[1]
            }
            if ($DestinationFile -and !(Split-Path $DestinationFile)) {
                $DestinationFile = Join-Path (Get-Location -PSProvider "FileSystem") $DestinationFile
            }
            if ($DestinationFile) {
                $fileDirectory = $([System.IO.Path]::GetDirectoryName($DestinationFile))
                if (!(Test-Path($fileDirectory))) {
                    [System.IO.Directory]::CreateDirectory($fileDirectory) | Out-Null
                }
            }

            # Create async Bitsjob and monitor progress
            ###################################################
            $bitsJob = Start-BitsTransfer -DisplayName $fileName -Source $URL -Asynchronous -Destination $DestinationFile -TransferType Download -Priority Foreground
            $Starttime = $bitsJob.CreationTime
            Write-Verbose "[ $Starttime ] Downloading $fileName"

            # Count variable variable against high-frequency updates
            ###################################################
            $lastPercent = 0

            while (($bitsJob.JobState -eq "Transferring") -or ($bitsJob.JobState -eq "Connecting") -or ($bitsJob.JobState -eq "TransientError") -or ($bitsJob.JobState -eq "Suspended") ) {

                $percentComplete = [int](($bitsJob.BytesTransferred * 100) / $bitsJob.BytesTotal)

                if (($percentComplete % 4 -eq 0) -and ($percentComplete -ne $lastPercent)) {
                    if ((Test-UI) -or (Test-PipeServer)) {
                        Update-OSDProgress -PercentComplete $percentComplete
                    }
                    else {
                        # Console output if ui closed
                        ###################################################
                        Write-Host -NoNewLine "`rDownload $fileName, complete : [$($percentComplete.ToString("##0.00").PadLeft(6)) % ]"
                        if ($percentComplete -eq 100) { Write-Host "`n" }
                    }
                    $lastPercent = $percentComplete
                }
            }
            Switch ($bitsJob.JobState) {
                "Transferred" {
                    Complete-BitsTransfer -BitsJob $bitsJob ; break
                }
                "Error" {
                    throw "$($bitsJob.JobState) on $fileName" ; break
                }
                default { throw "Strange Error $($bitsJob.JobState) on $fileName" ; break } # Perform corrective action.
            }
        }

        catch {
            $ExeptionMsg = $_.Exception.Message
            Write-Host "Download breaks with error : $ExeptionMsg"
        }

        finally {}
    }

    End {
        Update-OSDProgress -HideBar -DownloadFile " "
    }
}