如何解决PowerShell 7.3中NuGet Provider与Find-Module安装错误
问题详情
运行包含Install-AWSToolsModule SecurityToken -Force的脚本时,触发以下系列报错:
初始提示
NuGet provider is required to continue
This version of PowerShellGet requires minimum version '2.8.5.201' of NuGet provider to publish an item to NuGet-based
repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\timothy.granata\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider
by running 'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install
and import the NuGet provider now?
[Y] Yes [N] No [S] Suspend [?] Help (default is "Y"):
输入Y后返回错误:
Find-Module: NuGet provider is required to interact with NuGet-based repositories. Please ensure that '2.8.5.201' or newer version of NuGet provider is installed.
手动执行推荐命令时报错:
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
输出:
Install-PackageProvider: Unable to find repository with SourceLocation ''. Use Get-PSRepository to see all available repositories.
运行Get-PSRepository同样报错:
Get-PackageSource: Unable to find module providers (PowerShellGet).
触发问题的脚本代码:
if (-not (Get-Module AWS.Tools.Installer -ListAvailable)) { Install-Module AWS.Tools.Installer -Force } Install-AWSToolsModule SecurityToken -Force Get-AWSCredential -ListProfileDetail | ForEach-Object { Remove-AWSCredentialProfile -ProfileName $_.ProfileName -Force }
已尝试的无效操作
- 重新安装PowerShell 7
- 执行
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12启用TLS 1.2 - 以管理员身份运行PowerShell
- 删除
C:\Users\<user>\Documents\WindowsPowerShell路径下的Modules文件夹
可行解决方案
方案1:手动部署NuGet Provider文件
- 获取版本≥2.8.5.201的NuGet Provider文件(
NuGet.psd1和NuGet.dll)。 - 将文件复制到以下任一目录:
- 全局路径:
C:\Program Files\PackageManagement\ProviderAssemblies(需管理员权限) - 用户路径:
C:\Users\<你的用户名>\AppData\Local\PackageManagement\ProviderAssemblies
- 全局路径:
- 重启PowerShell 7后重试脚本。
方案2:修复PowerShellGet与PackageManagement模块
以管理员身份启动PowerShell 7,依次执行以下命令:
# 卸载现有损坏模块 Uninstall-Module -Name PowerShellGet -AllVersions -Force Uninstall-Module -Name PackageManagement -AllVersions -Force # 重新安装最新版本 Install-Module -Name PowerShellGet -Force -AllowClobber Install-Module -Name PackageManagement -Force -AllowClobber # 安装NuGet Provider Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
方案3:手动注册PSGallery仓库
若仓库丢失,先注册默认仓库再安装:
# 注册PSGallery仓库 Register-PSRepository -Default # 指定仓库安装NuGet Provider Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Repository PSGallery
内容的提问来源于stack exchange,提问作者Timothy G.

