Search code examples
powershellmicrosoft-bits

Start-BitsTransfer is ignoring filename when redirecting


I'm using the Start-BitsTransfer command to download remote resources in powershell scripts.

However, it seems that the command does not take the correct filename when the url is a short url.

For example, these url: http://ligman.me/1IW1oab redirect actually to http://download.microsoft.com/DOWNLOAD/D/6/7/D670D322-5771-409E-BF34-5B98496DEB0A/MICROSOFT_PRESS_EBOOK_INTRODUCING_AZURE_PDF.PDF (HTTP 301 response).

But when I execute

Start-BitsTransfer http://ligman.me/1IW1oab

The result filename is 1IW1oab

Is there a way to use this command and the obtain the right filename?


Solution

  • Simply using the command no but you can resolve before the shot url in this way:

    $url = 'http://ligman.me/1IW1oab'    
    $WebClientObject = New-Object System.Net.WebClient
    $WebRequest = [System.Net.WebRequest]::create($URL)
    $WebResponse = $WebRequest.GetResponse()
    $ActualDownloadURL = $WebResponse.ResponseUri.AbsoluteUri
    $ObjectProperties = @{ 'Shortened URL' = $URL;
                           'Actual URL' = $ActualDownloadURL}
    $ResultsObject = New-Object -TypeName PSObject -Property $ObjectProperties
    $WebResponse.Close()
    $ResultsObject.'Actual URL'