Powershell The request was aborted: Could not create SSL/TLS secure channel. (请求被中止: 未能创建 SSL/TLS 安全通道。)错误最近发生在在 Windows 7 执行 Invoke-RestMethod 时。- PS C:\Users\wuxiancheng\Downloads> wuxiancheng.ps1 -Verbal
- Invoke-RestMethod : The request was aborted: Could not create SSL/TLS secure channel.
- At C:\apps\cmd\wuxiancheng.ps1:113 char:21
- + $Response = Invoke-RestMethod @InvokingArguments
- + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
- + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
复制代码 这个问题的产生,是因为 Windows 7 上 .Net 默认不启用高版本 TLS 支持,而现代网站通常只支持高版本 TLS。要解决这个问题,可以改系统注册表,也可以改 Powershell 代码。
方法一、改注册表。需要以管理员身份运行以下 Powershell 代码。- Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value 1 -Type DWORD
- Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value 1 -Type DWORD
复制代码 方法二、改 Powershell 代码。在代码前面添加以下代码。- $SecurityProtocolTypes = 0
- [Enum]::GetValues([Net.SecurityProtocolType]) | Where-Object {
- $_ -NotIn @([Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::SystemDefault)
- } | ForEach-Object {
- $SecurityProtocolTypes = $SecurityProtocolTypes -bor $_
- }
- [Net.ServicePointManager]::SecurityProtocol = $SecurityProtocolTypes
复制代码 允许使用 SSl3、TLS 1.0 以外的所有版本。如果要允许 TLS 1.0,将上述代码中的以下内容去掉即可。
- [Net.SecurityProtocolType]::Tls,
复制代码 相关帖子:PowerShell Gallery 已经不支持 TLS 1.0 和 1.1 |
|