使用基本身份验证的 Powershell 下载文件

Jel*_*lte 5 powershell

我正在尝试使用 PowerShell 从我的 JFrog 存储库下载文件,但无法使其正常工作。

这是我得到的错误:

使用“2”个参数调用“DownloadFile”的异常:“底层连接已关闭:意外错误
发生在发送中。”
在 C:\Users\jelte\Documents\myproject\downloadExecutables1.ps1:16 char:1
+ $req.DownloadFile('https://myrepo.jfrog.io/myproject/libs-release-loca ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], 

方法调用异常

我使用的代码:

$usernameVar = "JFROG_USERNAME"
$username = (get-item env:$usernameVar).Value

$passwordVar = "JFROG_PASSWORD"
$password = (get-item env:$passwordVar).Value

$auth = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password))
$req = New-Object System.Net.WebClient
$req.Headers.Add('Authorization', $auth)
$installersFolder = $PSScriptRoot + '\prerequisites\executables'
If(!(test-path $installersFolder))
{
      New-Item -ItemType Directory -Force -Path $installersFolder
}

$req.DownloadFile('https://myrepo.jfrog.io/myproject/libs-release-local/binary/com/targetexecutable/9.3.240/executable-9.3.240.exe', $installersFolder + '\executable-9.3.240.exe')
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情:

  1. 从不安全的服务器下载文件,这是有效的。我怀疑基本身份验证出了问题。
  2. 我检查了用户名和密码是否填写,我还检查了 base64 哈希是否正确。
  3. 将 PowerShell 置于跟踪模式,这样我就可以获得更清晰的错误,但不走运。(为什么这些错误这么不清楚?)
  4. 使用Invoke-Webrequest,这有效,但速度非常慢。
  5. 使用 cURL 来检查它是否是其他东西,这也有效。

小智 1

尝试将这些行添加到脚本的开头。

$AllProtocols = [System.Net.SecurityProtocolType]'Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols 
Run Code Online (Sandbox Code Playgroud)

我遇到了类似的情况,但没有提供非常丰富的错误消息,问题显然只是我需要将 powershell 使用的安全协议设置为 Tls 1.2。