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 SourceForge and therefore might not be a good candidate to be redirected immediately.
The documentation of SourceForge states, that they offer a direct download, when using wget
for example:
We also have a special "latest/download" URL that's a bit like Google's "I'm Feeling Lucky" button for downloads:
$ wget https://sourceforge.net/projects/phppgadmin/files/latest/download
If you use it we’ll try to send you the latest download. Wget (and similar tools) makes it a little tricky to figure out if you’re on Windows, a Mac, Linux, etc. If you’re downloading something that’s OS-specific you’re better off using the full URL.
The default user agent of wget
looks like the following, where the number represents the current version of wget
:
OS | User agent |
---|---|
Windows | Wget/1.21.1 |
Linux | Wget/1.21.1 (linux-gnu) |
Using just Wget
as the user agent will already be enough to donwload the executable directly:
Invoke-WebRequest -UserAgent "Wget" -Uri https://sourceforge.net/projects/keepass/files/latest/download -OutFile $env:USERPROFILE'\Downloads\KeePass2-Latest.exe'
As KeePass only offers a Windows executable anyway, you don't have to worry about Linux or macOS binaries/packages.