简述
在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/'
PowerShellGet\Update-Module
最终将责任转移到了 PackageManagement\Install-Package
- 它正在查找位于 '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安装的所有模块似乎可以解决问题。
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”匹配的参数。
- JimPackageManagement
模块,而Install-Package
命令就在其中。我的版本目前是1.4.7。 - Jason Boydgmo -list PackageManagement
,发现在1.4.7版本旁边的列表中仍然有1.0.0.1(作为二进制文件),所以我想,也许那就是罪魁祸首。于是我去文件夹里手动删除了1.0.0.1文件夹,之后就可以正常工作了,在启动VSC时不再收到该消息。 - SuperDre