简体   繁体   English

如何使用 Powershell 从 sourceforge 下载文件?

[英]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.我正在尝试使用 powershell 在几台计算机上自动更新 KeePass,所以我对某种从 sourceforge 自动下载最新版本的方法很感兴趣,但是我无法让它工作。 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.这两者都下载 Web 的页面文件,而不是安装程序本身。

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. "根据此 Sourceforge 帖子常规下载链接将起作用,只要下载功能能够遵循重定向,并且看起来不是浏览器源(例如,没有类似浏览器的 User-Agent String ),它只会重定向到文件本身。

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.但是它对我不起作用,我没有指定任何 UserAgent,我什至尝试使用-UserAgent $null作为参数,但没有运气。

Is there a way to programatically download from sourceforge using command-line/Powershell?有没有办法使用命令行/Powershell 以编程方式从 sourceforge 下载?

According to the documentation, the default user agent of Invoke-WebRequest is similar to:根据文档, Invoke-WebRequest的默认用户代理类似于:

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.这可能会被https://sourceforge.net/解释为浏览器用户代理,因此可能不是立即重定向的好选择。

Try any custom string like StackOverflow :尝试任何自定义字符串,如StackOverflow

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

This will work.这将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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