powershell无法创建ssl/tsl安全

powershell无法创建ssl/tsl安全,powershell,Powershell,我正在使用powershell脚本下载并执行一个文件,但由于一段时间后我遇到了一个无法创建ssl/tsl安全通道的问题 $down = New-Object System.Net.WebClient; $url = 'url'; $file = 'file'; $down.DownloadFile($url,$file); $exec = New-Object -com shell.application; $exec.shellexecute($file); exit; 可能是您

我正在使用powershell脚本下载并执行一个文件,但由于一段时间后我遇到了一个无法创建ssl/tsl安全通道的问题

$down = New-Object System.Net.WebClient; 
$url = 'url'; 
$file = 'file';
$down.DownloadFile($url,$file); 
$exec = New-Object -com shell.application; 
$exec.shellexecute($file); 
exit; 

可能是您连接的站点需要TLS 1.2,而powershell默认使用TLS 1.0(如果我没记错的话)

如果不使用Tls 1.2,我会出现以下错误:

Exception calling "DownloadFile" with "2" argument(s): "The request was aborted: Could not create SSL/TLS
secure channel."
At line:1 char:1
+ $down.DownloadFile($url,$file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

在Windows server中安装Wiki.js时,我遇到了相同的错误。问题是ps1脚本将TLS 1.1作为后备方案包括在内。对于任何其他powershell安装,可以更改以下步骤

解决这个问题

  • 我从上的安装说明下载了install.ps1文件

    iex((新对象系统.Net.WebClient).DownloadString(“”))

  • 从第一行中删除“tls11,tls”

    发件人:

    [Net.ServicePointManager]::SecurityProtocol=“tls12,tls11,tls”

    致:

    [Net.ServicePointManager]::SecurityProtocol=“tls12”

  • 将文件保存在本地目录中,并将目录(CD)更改为 本地目录运行了该命令 “iex.\install.ps1”


  • 现在一切都好了。

    我之前也遇到过同样的问题,我是如何通过更改链接来解决的。例如,确保您尝试下载的页面是原始文件-

    https://raw.githubusercontent.com/TTT2866/Batch-username-generator/master/username_generator.bat

    而不是

    https://github.com/TTT2866/Batch-username-generator/blob/master/username_generator.bat


    请注意,第一个链接中的“原始”表示此示例代码。几年前Terraform搬到TLS时,我写了这篇文章

    $source=<folder where file suppose to be present>
    Write-Verbose -Verbose "Downloading Terraform Required"
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12
    $wc = New-Object System.Net.WebClient
    if ((test-path "${source}\terraform.zip") -eq $false) {
        $wc.downloadfile("https://releases.hashicorp.com/terraform/0.11.2/terraform_0.11.2_windows_amd64.zip","${source}\terraform.zip")
    }
    Add-Type -assembly "system.io.compression.filesystem"
    [io.compression.zipFile]::ExtractToDirectory("$source\terraform.zip", $destination)
    
    $source=
    Write Verbose-Verbose“需要下载Terraform”
    [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::TLS12
    $wc=新对象System.Net.WebClient
    if((测试路径“${source}\terraform.zip”)-eq$false){
    $wc.downloadfile(“https://releases.hashicorp.com/terraform/0.11.2/terraform_0.11.2_windows_amd64.zip“,“${source}\terraform.zip”)
    }
    添加类型-assembly“system.io.compression.filesystem”
    [io.compression.zipFile]::ExtractToDirectory($source\terraform.zip,$destination)
    
    应启用TLS 1.2以使其正常工作。在PowerShell中,您可以通过运行以下代码了解系统支持哪些协议:

    [Enum]::GetNames([Net.SecurityProtocolType]) -contains 'Tls12'
    
    如果结果为真,则系统支持TLS 1.2。通过运行以下命令,您可以了解正在使用哪些协议:

    [System.Net.ServicePointManager]::SecurityProtocol.HasFlag([Net.SecurityProtocolType]::Tls12)
    
    如果结果为真,则使用TLS 1.2。但是,您可以使用以下命令显式添加TLS 1.2:

    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
    

    这应该可以解决这些问题。

    使用“2”参数调用“DownloadFile”时出现异常:“WebClient请求期间发生异常。”在第5行char:1+$down.DownloadFile($url,$file)我现在遇到此错误,我正在使用github托管我的文件并直接下载it@katemaran
    $url
    是否使用
    http
    https
    ?当您手动将url粘贴到web浏览器中时会发生什么情况?该网站使用https它是Github,如果我粘贴url它将下载file@katemaran真奇怪。请参阅编辑-仅使用git文件对其进行了测试;适用于我的Tls 1.2,不是没有。您需要提供guthub凭据吗?路径有效且目录存在吗?它在没有指定路径的情况下工作,我上面使用的脚本中的默认下载路径是什么?另外,如果Matters试图以巧克力方式安装并运行到该错误,则我使用的文件是可执行文件。这起作用了。谢谢你的帮助。这个答案应该被接受。很高兴听到这个消息。
    [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12