简体   繁体   中英

How to download a file from sourceforge using Powershell?

I am trying to automate updating KeePass in a few computers using powershell, so I'm interested in some sort of way to automatically download the latest release from sourceforge, however I cannot make it work. I've seen a lot of references by googling but none work for me. I'm trying this at the current time:

$url=https://sourceforge.net/projects/keepass/files/latest/download
#$url=https://sourceforge.net/projects/keepass/files/KeePass%202.x/2.49/KeePass-2.49-Setup.exe

Invoke-WebRequest -Uri $url -OutFile $env:USERPROFILE'\Downloads\KeePass2-Latest.exe'

Both of these download the web's page file, not the installer itself.

According to this Sourceforge post " The regular download link will work, as long as the download function is able to follow redirects, and does not appear to be a browser source (eg., doesn't have a browser-like User-Agent String), it will simply redirect to the file itself. "

However it's not working for me, I'm not specifying any UserAgent, and I've even tried to use -UserAgent $null as a parameter, but no luck.

Is there a way to programatically download from sourceforge using command-line/Powershell?

According to the documentation, the default user agent of Invoke-WebRequest is similar to:

Mozilla/5.0 (Windows NT 10.0; Microsoft Windows 10.0.15063; en-US) PowerShell/6.0.0

This might be interpreted as a browser user agent by https://sourceforge.net/ and therefore might not be a good candidate to be redirected immediately.

Try any custom string like StackOverflow :

Invoke-WebRequest -UserAgent "StackOverflow" -Uri https://sourceforge.net/projects/keepass/files/latest/download -OutFile $env:USERPROFILE'\Downloads\KeePass2-Latest.exe'

This will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM