通过PowerShell脚本安装NuGet

Bal*_*tar 25 powershell nuget powershell-3.0

据我所知,NuGet应该作为Visual Studio扩展安装:

http://docs.nuget.org/docs/start-here/installing-nuget
Run Code Online (Sandbox Code Playgroud)

但是如果我在没有安装VS的机器上需要NuGet呢?

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

小智 109

  1. 以管理员权限运行 Powershell
  2. 为 TLS12 键入以下 PowerShell 安全协议命令:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Run Code Online (Sandbox Code Playgroud)

  • @Lee_Dailey 因为这是一个比带外下载和安装 nuget 更容易的解决方案,并且在某种程度上解释了它失败的原因,而不仅仅是如何解决它。 (2认同)
  • @MarkHenderson - 谢谢你......这让我很困惑。[*咧嘴笑*] (2认同)

Yan*_*nko 39

这是一个简短的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
Run Code Online (Sandbox Code Playgroud)

请注意,Invoke-WebRequestcmdlet随PowerShell v3.0到达.本文给出了这个想法.

  • 我更新了脚本以获取最新的NuGet.exe,这里:[https://dist.nuget.org/win-x86-commandline/latest/nuget.exe](https://dist.nuget.org/win-x86 -CommandLine /最新/ nuget.exe) (3认同)

小智 10

这似乎也是这样.PS示例:

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Run Code Online (Sandbox Code Playgroud)

  • 注意:2020 年 4 月 3 日左右,提供商查找站点上提出了最低 TLS 版本,如果您的计算机尚未通过策略设置,您可以使用 [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType ]'Tls11,Tls12' (23认同)

Sea*_*ice 8

如果没有Visual Studio,您可以从以下网址获取Nuget:http://nuget.org/nuget.exe

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

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


kno*_*cte 6

使用 PowerShell 但无需创建脚本:

Invoke-WebRequest https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile Nuget.exe
Run Code Online (Sandbox Code Playgroud)