通过PowerShell脚本安装NuGet

70
据我所知,NuGet 旨在作为 Visual Studio 扩展安装:
http://docs.nuget.org/docs/start-here/installing-nuget

如果我需要在一台没有安装VS的机器上使用 NuGet 呢?

具体来说,我想通过 PowerShell 脚本安装 NuGet。


NuGet现在是一个独立的程序,不应被视为任何类型的扩展。 - pizycki
6个回答

128
  1. 以管理员权限运行PowerShell
  2. 输入以下 PowerShell 安全协议命令以启用 TLS12:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

错误:无法设置属性。此语言模式仅支持核心类型的属性设置。 - HackSlash

58

这里有一段简短的 PowerShell 脚本,可以完成你预期的操作:

$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
$targetNugetExe = "$rootPath\nuget.exe"
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
Set-Alias nuget $targetNugetExe -Scope Global -Verbose

请注意Invoke-WebRequest cmdlet是PowerShell v3.0中引入的。 这篇文章提供了相关概念。


3
我更新了脚本以获取最新的NuGet.exe,网址在这里:https://dist.nuget.org/win-x86-commandline/latest/nuget.exe。 - Phobis
Invoke-WebRequest在PS 6.0之前的版本中应该使用-UseBasicParsing。 - Palec

25

这似乎也可以做到。 PS 示例:

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

但是你如何获得比2更新的版本? - Brain2000
27
注意:在2020年4月3日左右,提供程序查找站点的最低TLS版本已经提高,如果您的计算机没有通过策略进行设置,您可以使用 [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls11,Tls12' 进行设置。 - Jules Clements
使用Install-PackageProvider -Name NuGet -Scope CurrentUser以无管理员权限使用NuGet。该命令不适用于Powershell Core。 - KargWare
@JulesClements 谢谢,你帮我省了几个小时。 - Pavel Anikhouski

10

感谢回复。您知道是否有特定版本的 NuGet.exe 的链接可用吗? - BaltoStar
嗯,我不知道在哪里找到与nuget相关版本的命令。但是我猜如果你需要知道你有哪个版本的nuget.exe,只需输入“nuget help”。这可以帮助进一步搜索命令兼容性。 - SeanPrice
值得注意的是,http://nuget.org/nuget.exe 上的版本与 https://dist.nuget.org/win-x86-commandline/latest/nuget.exe 上的版本相比较老。 - Alex Angas
不到三年后,值得注意的是https://www.nuget.org/downloads。 - RolfBly

6

使用PowerShell,但不需要创建脚本:

Invoke-WebRequest https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile Nuget.exe

3
没有上述解决方案适用于我,我找到了一篇文章来解释这个问题。系统上的安全协议已经过时,因此显示了一个错误消息,表明未找到ProviderPackage的匹配项。此处是一篇文章。
以下是升级您的安全协议的基本步骤:
运行这两个 cmdlet 以设置.NET Framework 强密码学注册表键。然后重启 PowerShell 并检查是否添加了安全协议 TLS 1.2。最后,安装 PowerShellGet 模块。
第一个 cmdlet 是在64位 .Net Framework(版本为4及以上)上设置强密码学。
[PS] C:\>Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
1
[PS] C:\>Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
The second cmdlet is to set strong cryptography on 32 bit .Net Framework (version 4 and above).

[PS] C:\>Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
1
[PS] C:\>Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord
Restart Powershell and check for supported security protocols.

[PS] C:\>[Net.ServicePointManager]::SecurityProtocol
Tls, Tls11, Tls12
1
2
[PS] C:\>[Net.ServicePointManager]::SecurityProtocol
Tls, Tls11, Tls12
Run the command Install-Module PowershellGet -Force and press Y to install NuGet provider, follow with Enter.

[PS] C:\>Install-Module PowershellGet -Force
 
NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\administrator.EXOIP\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

[PS] C:\>Install-Module PowershellGet -Force
 
NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or
'C:\Users\administrator.EXOIP\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

我从未被要求安装NuGet。也许是因为我已经在运行上述PS命令的目录中拥有nuget.exe? - Fandango68

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