当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell:无法解析包源

作者:精品下载站 日期:2024-12-14 23:36:38 浏览:11 分类:玩电脑

PowerShell:无法解析包源


当您尝试从在线 PSGallery 安装 PowerShell 模块时,您可能会看到以下错误:

Install-Module SqlServer
WARNING: Unable to resolve package source 'https://www.powershellgallery.com/api/v2'.
PackageManagement\Find-Package : No match was found for the specified search criteria and module name 'sqlserver. Try Get-PSRepository to see all available registered module repositories.
NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage

[玩转系统] PowerShell:无法解析包源

使用 Nuget 提供程序安装任何 PowerShell 模块时都会出现类似的错误,无论它是什么:Exchange Online Powershell 模块、AzureAD、VMware PowerCLI、PSWindowsUpdate、SecretManagement、SQLServer 等。同时,您可以手动安装任何离线 PowerShell 模块。

该问题可能会出现在 Windows 10、Windows Server 2016 或以前的 Windows 版本上。原因是 PowerShell 默认尝试使用旧版且不安全的 TLS 1.0 协议连接到 PSGallery 存储库。自 2020 年 4 月起,PowerShell Gallery 接受仅使用 TLS 1.2 连接到 NuGet 提供程序。

如果 Windows 中未禁用 TLS 1.0 和 TLS 1.1,请运行以下命令以在当前 PowerShell 会话中使用 TLS 1.2:

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

从 Windows 8 和 Windows Server 2012 开始的所有 Windows 版本都支持 TLS 1.2。您可以在 PowerShell 中显示支持的 TLS 版本列表:

[enum]::GetNames([Net.SecurityProtocolType])

[玩转系统] PowerShell:无法解析包源

为了在 Windows 7 SP1/Windows Server 2008 R2 中启用 TLS 1.2 支持,请安装 KB3140245 和 MicrosoftEasyFix51044.msi 修复程序。

如果您的 Windows 7 不支持 TLS 1.2,您将看到错误 0x800CCC1A — 如果您尝试使用 Outlook 连接到任何现代电子邮件服务,您的服务器不支持您指定的加密类型。

现在您可以从 PowerShell Gallery 安装或更新模块:

[玩转系统] PowerShell:无法解析包源

下次打开 PowerShell 控制台时,您必须再次启用 TLS 1.2 协议。您可以显示 PowerShell 中用于连接的协议版本,如下所示:

[Net.ServicePointManager]::SecurityProtocol

[玩转系统] PowerShell:无法解析包源

为了避免每次打开 PowerShell 时启用 TLS 1.2,您可以更改 .NET Framework 的默认 TLS 版本。为此,请使用 PowerShell 对注册表进行以下更改:

Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

如果您的计算机通过代理服务器连接到 Internet,请确保在 PowerShell 会话中配置代理连接:

netsh winhttp show proxy

您可以从 Windows 导入代理服务器设置:

netsh winhttp import proxy source=ie

上一篇文章描述了安装 PowerShell 模块时出现的类似错误 Unable to download from URI。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯