Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, August 22, 2013 4:51 AM | 1 vote
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!
All replies (6)
Thursday, August 22, 2013 2:06 PM âś…Answered | 3 votes
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
Thursday, August 22, 2013 5:57 AM
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")
Thursday, August 22, 2013 6:20 AM
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?
Thursday, August 22, 2013 9:59 AM
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.
Wednesday, April 27, 2016 8:20 PM
Helped me years later, thanks!
Shawn Keene
Saturday, April 22, 2017 2:52 PM
Shorter answer can be -UserAgent "NativeHost" (which is the default string on WebClient)
GitHub: https://github.com/saschanaz/