> ## 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.

# Could not create SSL/TLS secure channel

> 透過在工作階段中或系統範圍啟用 TLS 1.2，修復 PowerShell『Could not create SSL/TLS secure channel』錯誤，使 Invoke-WebRequest 指令稿可正常運作。

## 問題

當執行使用 `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，它們預設使用現代的安全通訊協定。
