Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PowerShell从公共Github下载exe_Powershell - Fatal编程技术网

PowerShell从公共Github下载exe

PowerShell从公共Github下载exe,powershell,Powershell,我无法从Github下载.exe文件。该脚本适用于不同的站点,下载文件时不会出现问题 这是我正在尝试下载的.exe: 完整脚本: [Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3" $DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe" $WebRespons

我无法从Github下载.exe文件。该脚本适用于不同的站点,下载文件时不会出现问题

这是我正在尝试下载的.exe:

完整脚本:

[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"
$DownloadUrl = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$WebResponse = Invoke-WebRequest -Uri "$DownloadUrl" -Method Head
Write-Output "Downloading $DownloadUrl"
Start-BitsTransfer -Source $WebResponse.BaseResponse.ResponseUri.AbsoluteUri.Replace("%20", " ") -Destination "C:\Users\Pegavo\Desktop\PS\"

使用HEAD有什么原因吗?GET似乎可以工作

您只需使用
调用WebRequest
-OutFile
参数即可

Invoke-WebRequest https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe -OutFile "ShareX-13.1.0-setup.exe"
此命令将从GitHub下载文件,并将结果存储到当前目录中的文件中

或者,您可以使用
WebClient

$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile("https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe", "E:\your\path\ShareX-13.1.0-setup.exe")
我在本地机器上测试了这两个。两者都起作用了

此外,如果您想了解为什么Start BitsTransfer不起作用


编辑: 您可以使用
分割路径
,像这样自动获取文件名:

$url = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$file= Split-Path $url -Leaf #file is ShareX-13.1.0-setup.exe now
Invoke-WebRequest $url -OutFile $file

GET不起作用,我试过了<代码>开始比特串:在源参数或目标参数中指定的值不正确。验证源和目标参数中的目录和文件名是否正确。在第1行char:1中,当我手动命名下载的文件时,
“C:\Users\Pegavo\Desktop\PS\sharex.exe”
我收到此错误:
开始BitsTransfer:HTTP状态403:客户端对请求的服务器对象没有足够的访问权限。
是否有一种简单的方法可以获取文件名并将其输出,而无需手动将其写入脚本?就像它用于
$WebResponse.BaseResponse.ResponseUri.AbsoluteUri.Replace(“%20”和“”)
或者唯一的方法是使用split/regex将正确的文件名发送到目标?我已经更新了答案。您可以使用
分割路径
获取扩展名为的文件名。现在再看看它是否适用于您的情况。如果我的回答被接受,请记住将其标记为已接受!:D谢谢。删除引号:
“$DownloadUrl”
应该是
$DownloadUrl
$url = "https://github.com/ShareX/ShareX/releases/download/v13.1.0/ShareX-13.1.0-setup.exe"
$file= Split-Path $url -Leaf #file is ShareX-13.1.0-setup.exe now
Invoke-WebRequest $url -OutFile $file