Steve B
Steve B

Reputation: 37660

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?

Upvotes: 1

Views: 1394

Answers (2)

PESMITH_MSFT
PESMITH_MSFT

Reputation: 358

The Start-BitsTransfer command includes a -destination switch

Start-BitsTransfer http://ligman.me/1IW1oab -destination AZURE.PDF

In general, for each "url shortening" scenario where using the final URL is the right choice, there's a "redirect for security" scenario that converts a perfectly fine URL into a GUID.

Upvotes: 0

CB.
CB.

Reputation: 60918

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'

Upvotes: 1

Related Questions