无法在Update-Module中找到存储库

35

我正在使用 Windows 10 和 Powershell 5.1

Get-PSRepository 的结果如下:

PSGallery Untrusted https://www.powershellgallery.com/api/v2

而 Update-Module 则返回错误信息:

PackageManagement\Install-Package:无法找到存储库 'https://www.powershellgallery.com/api/v2/'。请使用 Get-PSRepository 命令查看所有可用的存储库。 在 C:\Program Files\WindowsPowerShell\Modules\powershellget\2.0.1\PSModule.psm1:13000 字符:20 + ... $sid = PackageManagement\Install-Package @PSBoundParameters + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex ception + FullyQualifiedErrorId : SourceNotFound,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

有任何修复方法吗?

10个回答

72

简述

在PowerShell中注册的PSGallery仓库的URL看起来以前指向https://www.powershellgallery.com/api/v2/,但在某个时候被更改为https://www.powershellgallery.com/api/v2(注意末尾缺少斜杠)。

λ  Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Untrusted            https://www.powershellgallery.com/api/v2

从旧的URL安装的任何模块现在都无法更新。从PowerShell gallery重新安装它们将更新仓库URL,使模块可以正常更新。您可以使用以下命令重新安装指向旧URL的所有模块:

任何从旧URL安装的模块现在无法更新。从PowerShell gallery重新安装这些模块会更新仓库URL,允许模块正常更新。您可以使用以下命令重新安装所有指向旧URL的模块:

Get-InstalledModule `
| ? { $_.Repository -eq 'https://www.powershellgallery.com/api/v2/' } `
| % { Install-Package -Name $_.Name -Source PSGallery -Force -AcceptLicense }

完整的运行状况

我自己也遇到了这个非常恼人的问题。从错误消息中我们可以看到几件事情:

PackageManagement\Install-Package:无法找到存储库'https://www.powershellgallery.com/api/v2/'

  1. PowerShellGet\Update-Module 最终将责任转移到了 PackageManagement\Install-Package
  2. 它正在查找位于 'https://www.powershellgallery.com/api/v2/' 的存储库。

在我的计算机上运行 Get-PSRepository 的结果如下:

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Trusted              https://www.powershellgallery.com/api/v2

看起来仓库已经存在了,但是也许并不是这样。请注意末尾的正斜杠。是不是 Install-Package 正在寻找一个 SourceLocation 与该字符串完全匹配的仓库?让我们尝试更改 PSGallery 的 SourceLocation

Set-PSRepository -Name PSGallery -SourceLocation https://www.powershellgallery.com/api/v2/ -InstallationPolicy Trusted
PackageManagement\Set-PackageSource:PSGallery存储库具有预定义的位置。 'Location、NewLocation或SourceLocation' 参数不允许使用,请在删除'Location、NewLocation或SourceLocation' 参数后重试。位置:C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\2.0.4\PSModule.psm1:11768字符:17+ ... $null = PackageManagement\Set-PackageSource @PSBoundParameters + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument:(https://www.pow...ery.com/api/v2/:String) [Set-PackageSource],异常+ FullyQualifiedErrorId:ParameterIsNotAllowedWithPSGallery,Add-PackageSource,Microsoft.PowerShell.PackageManagement.Cmdlets.SetPackageSource
Register-PSRepository -Name PSGallery1 -SourceLocation https://www.powershellgallery.com/api/v2/ -InstallationPolicy Trusted
Update-Module -Name pester -Force

看,没有错误。它正常工作!

有趣的是,如果我列出已安装的模块列表,我会发现其中包含不同来源的存储库:

Get-InstalledModule | Select Name, Repository | FT -AutoSize

Name                         Repository
----                         ----------
7Zip4Powershell              PSGallery 
AWSPowerShell                PSGallery 
cChoco                       PSGallery1
dbatools                     PSGallery 
DLMAutomation                PSGallery1
InvokeBuild                  PSGallery1
Microsoft.PowerShell.Archive PSGallery1
PackageManagement            PSGallery 
Pester                       PSGallery1
posh-git                     PSGallery1
powershell-yaml              PSGallery1
PowerShellGet                PSGallery 
PowerUpSQL                   PSGallery1
psake                        PSGallery1
PsHosts                      PSGallery1
psTrustedHosts               PSGallery1
ReverseDSC                   PSGallery1
SeeShell                     PSGallery1
SqlServer                    PSGallery1
TunableSSLValidator          PSGallery1
xSmbShare                    PSGallery1
xWebAdministration           PSGallery1

看看安装自PSGallery1的所有模块,该库与https://www.powershellgallery.com/api/v2/相关!在刚才之前,我的计算机上从未安装过名为PSGallery1的存储库;我安装的每个模块都是来自PSGallery。我猜测PSGallery存储库曾经指向https://www.powershellgallery.com/api/v2/,但在某个时候,无论是有意还是无意地将其更改为https://www.powershellgallery.com/avp/v2,从而破坏了任何从先前URL安装的Update-Module模块。我猜想,如果我使用从更新的PSGallery存储库中的Install-Package重新安装模块,则一切都会得到解决,我可以删除PSGallery1存储库。

让我们更新所有从旧URL(PSGallery1)部署的模块:

Get-InstalledModule `
| ? { $_.Repository -eq 'PSGallery1' } `
| % { Install-Package -Name $_.Name -Source PSGallery -Force -AcceptLicense }

再次运行 Get-InstalledModule 会得到如下结果:

Name                         Repository
----                         ----------
7Zip4Powershell              PSGallery
AWSPowerShell                PSGallery
cChoco                       PSGallery
dbatools                     PSGallery
DLMAutomation                PSGallery
InvokeBuild                  PSGallery
Microsoft.PowerShell.Archive PSGallery
PackageManagement            PSGallery
Pester                       PSGallery
posh-git                     PSGallery
powershell-yaml              PSGallery
PowerShellGet                PSGallery
PowerUpSQL                   PSGallery
psake                        PSGallery
PsHosts                      PSGallery
psTrustedHosts               PSGallery
ReverseDSC                   PSGallery
SeeShell                     PSGallery
SqlServer                    PSGallery
TunableSSLValidator          PSGallery
xSmbShare                    PSGallery
xWebAdministration           PSGallery

太好了!现在让我们尝试移除PSGallery1仓库并更新模块:

Unregister-PSRepository PSGallery1
Update-Module -Name pester -Force

成功!模块已经无错误更新。

我不确定这里具体出了什么问题,是PSGallery存储库的URL还是Install-Package,但重新安装从旧URL安装的所有模块似乎可以解决问题。


3
在 PowerShell 5.1 桌面版中,-AcceptLicense 参数未知。移除它后,工作正常。名称 值
PSVersion 5.1.19041.1023 PSEdition 桌面版 PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.19041.1023 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1
- Jim
这是错误信息: Install-Package:找不到与参数名称“AcceptLicense”匹配的参数。 - Jim
1
@Jim 你可能使用的是旧版本的 PackageManagement 模块,而 Install-Package 命令就在其中。我的版本目前是1.4.7。 - Jason Boyd
我也遇到了这个问题,在Visual Studio Code PowerShell扩展中收到了它发现旧版本的消息。但即使我做了以上所有操作,完美地解决了问题,我仍然收到了这个消息。然后我使用了gmo -list PackageManagement,发现在1.4.7版本旁边的列表中仍然有1.0.0.1(作为二进制文件),所以我想,也许那就是罪魁祸首。于是我去文件夹里手动删除了1.0.0.1文件夹,之后就可以正常工作了,在启动VSC时不再收到该消息。 - SuperDre

10

8

我也遇到了同样的问题,并找到了这个问题。我尝试了Jason Boyd(上面的)提到的一切,但没有起作用。

再搜索一些,发现了这个链接https://community.spiceworks.com/topic/2265662-powershell-get-download-problem

其中说到TLS 1.0可能是罪魁祸首。 它建议运行

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

然后,我成功更新了我的软件包。


2
以上答案的组合对我有用。
PS> [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 
PS> Install-PackageProvider Nuget –Force
PS> Install-Module -Name PSWindowsUpdate

安装新版本前可能需要先删除旧版本的PSWindowsUpdate

您可以使用-force参数进行安装,这将同时安装两个版本,但这可能不是最佳选择。


1
我遇到了与Windows Powershell 5.1.17134.407相同的问题,也在PowerShell 6.1上测试过。Update-Module在PowerShell 6.1中按预期工作,并且在Windows PowerShell和PowerShell中使用相同版本的PowerShellGet模块。因此,看起来问题是唯一存在于Windows PowerShell中的,并且在运行时Update-Module代码内部出现了问题。
我没有使用Update-Module的解决方案,但可以使用Install-Module代替,并使用-AllowClobber参数。它不会像Update-Module那样出现错误。目前至少,最终结果将是相同的,因为根据我的测试和https://github.com/PowerShell/PowerShellGet/issues/213,Update-Module实际上只是与已安装的任何旧版本并排安装新版本。

...

经过进一步测试后,我恰好重新启动了正在测试的系统。重启后,Windows PowerShell 5.1中Update-Module的问题得到了解决 - Update-Module现在可以正常工作了。我不能确定重启是否解决了问题,但它现在已经解决了。


PSGallery仓库未经信任不会导致您在Update-Module中遇到的问题。当一个仓库未经信任时,默认行为是您将被提示批准从未经信任的仓库安装/更新模块。 - Mike Campbell
您可以使用Set-PSRepository cmdlet将存储库设置为可信。 - Mike Campbell

1
我尝试了这个方法,但是没有成功使用PSGaller2。因此,我继续寻找解决方案。由于我处于VPN /代理环境中,更新无法正常运行。当我执行此操作时,它对我有效。
$webclient=New-Object System.Net.WebClient
$webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

0

修复:

  1. Install-Module -Name PowershellGet -Repository PSGallery -Force
  2. 关闭现有的 PowerShell 或 ISE 会话并启动新会话
  3. Update-Module

0

我发现了类似的问题。在我的情况下,这是由于TLS所引起的。

我按照以下步骤解决了此问题: 1. 在64位.Net Framework(版本4及以上)上设置强加密 Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type Dword

  1. 在32位.Net Framework(版本4及以上)上设置强加密 Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type Dword

  2. 重新启动PS控制台

  3. 通过using[Net.ServicePointManager]::SecurityProtocol检查支持的协议

  4. 注册默认值 Register-PSRepository -Default

0

试试这个:

[System.Net.WebRequest]::DefaultWebProxy.Credentials = System.Net.CredentialCache]::DefaultCredentials

get-psrepository

register-psrepository -default

-2

对我来说,这是可行的代码:

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" 

$env:Path += ";C:\ProgramData\chocolatey\bin"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接