> ## Documentation Index
> Fetch the complete documentation index at: https://help.gorelo.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 无法创建 SSL/TLS 安全通道

> 通过在会话中或系统范围内为 Invoke-WebRequest 脚本启用 TLS 1.2，修复 PowerShell 中的 "Could not create SSL/TLS secure channel" 错误。

## 问题

在运行使用 `Invoke-WebRequest` 的 PowerShell 脚本时，可能会出现以下错误：

```powershell theme={null}
Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.
```

这是因为脚本尝试使用安全协议（TLS/SSL）连接到服务器，但系统上未启用所需的协议（如 TLS 1.2）。

<Frame>
  <img src="https://mintcdn.com/gorelo/k9Vf68jFwh3yoHvE/images/image-168.png?fit=max&auto=format&n=k9Vf68jFwh3yoHvE&q=85&s=6777e83ac2b24818bbb93b26b91317a6" alt="Image" width="1527" height="248" data-path="images/image-168.png" />
</Frame>

## 解决方案

<Tabs>
  <Tab title="临时">
    要在当前会话中快速修复此问题，你可以在 PowerShell 会话中运行以下命令以启用必要的协议：

    ```powershell theme={null}
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls11 -bor ` [Net.SecurityProtocolType]::Tls12 -bor ` [Net.SecurityProtocolType]::Tls -bor ` [Net.SecurityProtocolType]::Ssl3
    ```

    但是，此修复是临时的，需要在每个新的 PowerShell 会话中重新应用。
  </Tab>

  <Tab title="永久">
    要永久解决此问题，并确保所有基于 .NET 的应用程序（包括 PowerShell）默认使用安全协议，请更新系统注册表。

    ### 修复步骤：

    1. 打开**提升权限的 PowerShell 会话**（以管理员身份运行）。
    2. 运行以下命令以更新注册表：

    ```powershell theme={null}
    Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\.NETFramework\v4.0.30319" -Name "SchUseStrongCrypto" -Value 1 -Type DWord 
    Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\.NETFramework\v4.0.30319" -Name "SystemDefaultTlsVersions" -Value 1 -Type DWord
    ```

    3. 也对 64 位 .NET Framework 注册表项应用更改：

    ```text theme={null}
    Set-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v4.0.30319" -Name "SchUseStrongCrypto" -Value 1 -Type DWord 
    Set-ItemProperty -Path "HKLM:\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\v4.0.30319" -Name "SystemDefaultTlsVersions" -Value 1 -Type DWord
    ```

    4. 重启系统以确保更改生效。
  </Tab>
</Tabs>

## 注册表项说明

| 注册表设置                        | 描述                                      |
| ---------------------------- | --------------------------------------- |
| **SchUseStrongCrypto**       | 强制 .NET Framework 使用强加密协议（例如 TLS 1.2）。  |
| **SystemDefaultTlsVersions** | 确保 .NET 应用程序使用系统默认的 TLS 版本，使其能够适应较新的协议。 |

## 如果错误仍然存在

如果错误仍然存在，请考虑采取以下措施，以确保所有 .NET 应用程序都能安全连接，而无需在每个会话中手动干预：

* 确保你的系统支持 TLS 1.2，并已在操作系统中启用。

* 升级到 PowerShell Core 或 PowerShell 7，它们默认使用现代安全协议。
