powershell - Invoke-WebRequest - sourceforge urls/redirection/dynamic content

Hallo,

I try to use Invoke-WebRequest CmdLet in Powershell for downloading OpenOffie 4.0:

Invoke-WebRequest -Uri "http://downloads.sourceforge.net/project/openofficeorg.mirror/4.0.0/binaries/en-US/Apache_OpenOffice_4.0.0_Win_x86_install_en-US.exe?r=http%3A%2F%2Fwww.openoffice.org%2Fdownload%2Findex.html&ts=1375536623&use_mirror=heanet" -OutFile e:\Apache_OpenOffice_4.0.0_Win_x86_install_en-US.exe 

What I get is the website and not the file. Firefox follows the redirection and gets the correct file. How can I tell "Invoke-WebRequest" that I like to download the redirection an not the current website.

Thanks!

August 22nd, 2013 12:53am

Hi,

not sure why invoke-webrequest doesn't work in that case, but WebClient does the job:

$webclient = New-Object System.Net.WebClient
$webclient.DownloadFile($uri,"$destination")

Free Windows Admin Tool Kit Click here and download it now
August 22nd, 2013 2:00am

Yes thanks,

maybe I need to create some hybrid download function using Invoke-WebRequest in one case and WebClient in the other ... (I like to have the progress bar and do not want to reinvent it on my own.)

But the behavior of WebClient does not seem to be deterministic either, in some cases/urls it delivers the website as well ...

Is there a way of analyzing the header. And then making a decision what to do? Sourceforge is a problem it self, cause it uses strange mirror redirects. - Can sourceforge be used via FTP?

Any ideas?


August 22nd, 2013 2:22am

There are three ways to download from sourceforge:

Invoke-WebRequest (progress bar) - does not work

Start-BitsTransfer (prograss bar) - works

System.Net.WebClient.DownloadFile (no progress) - works

Start-BitsTransfer complains sometimes about the missing length of the file.

Free Windows Admin Tool Kit Click here and download it now
August 22nd, 2013 6:02am

Hi,

I've actually had another look and found that the redirects are not working with Invoke-Webrequest because of the default UserAgent value. If you specify another UserAgent it will follow the redirect and download the file as expected:

Invoke-WebRequest -Uri $url -OutFile $path -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::FireFox

August 22nd, 2013 10:07am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics