63

据我所知,NuGet 旨在作为 Visual Studio 扩展安装:

http://docs.nuget.org/docs/start-here/installing-nuget

但是,如果我在没有安装 VS 的机器上需要 NuGet 怎么办?

具体来说,我想使用 PowerShell 脚本安装 NuGet。

4

6 回答 6

114
  1. 以管理员权限运行 Powershell
  2. 为 TLS12 键入以下 PowerShell 安全协议命令:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
于 2020-04-16T08:47:01.993 回答
51

这是一个简短的 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-WebRequestcmdlet 随 PowerShell v3.0 一起提供。这篇文章给出了这个想法。

于 2014-10-17T08:43:31.580 回答
21

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

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
于 2018-01-11T22:03:43.507 回答
7

在没有 Visual Studio 的情况下,您可以从以下网址获取 Nuget:http: //nuget.org/nuget.exe

对于使用它的命令行执行,请查看:http ://docs.nuget.org/docs/reference/command-line-reference

对于 Powershell,只需将 nuget.exe 复制到机器上即可。无需安装,只需使用上述文档中的命令执行即可。

于 2013-05-20T20:37:36.933 回答
5

使用 PowerShell 但无需创建脚本:

Invoke-WebRequest https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile Nuget.exe
于 2020-04-02T06:20:43.267 回答
2

以上解决方案都不适合我,我找到了一篇解释该问题的文章。系统上的安全协议已被弃用,因此显示一条错误消息,提示未找到 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
于 2020-11-11T08:57:34.000 回答