简体   繁体   English

通过 PowerShell 脚本安装 NuGet

[英]Install NuGet via PowerShell script

As far as I can tell, NuGet is meant to be installed as a Visual Studio extension:据我所知,NuGet 旨在作为 Visual Studio 扩展安装:

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

But what if I need NuGet on a machine that doesn't have VS installed?但是如果我在没有安装 VS 的机器上需要 NuGet 怎么办?

Specifically, I would like to install NuGet with a PowerShell script.具体来说,我想用 PowerShell 脚本安装 NuGet。

  1. Run Powershell with Admin rights以管理员权限运行 Powershell
  2. Type the below PowerShell security protocol command for TLS12:为 TLS12 键入以下 PowerShell 安全协议命令:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

Here's a short PowerShell script to do what you probably expect:这是一个简短的 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

Note that Invoke-WebRequest cmdlet arrived with PowerShell v3.0.请注意, Invoke-WebRequest cmdlet 随 PowerShell v3.0 一起提供。 This article gives the idea. 这篇文章给出了思路。

This also seems to do it.这似乎也做到了。 PS Example: PS示例:

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

Without having Visual Studio, you can grab Nuget from: http://nuget.org/nuget.exe在没有 Visual Studio 的情况下,您可以从以下位置获取 Nuget: http ://nuget.org/nuget.exe

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

With respect to Powershell, just copy the nuget.exe to the machine.对于Powershell,只需将nuget.exe 复制到机器上即可。 No installation required, just execute it using commands from the above documentation.无需安装,只需使用上述文档中的命令执行即可。

使用 PowerShell 但无需创建脚本:

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

None of the above solutions worked for me, I found an article that explained the issue.上述解决方案都不适合我,我找到了一篇解释该问题的文章 The security protocols on the system were deprecated and therefore displayed an error message that no match was found for the ProviderPackage.系统上的安全协议已被弃用,因此会显示一条错误消息,指出找不到 ProviderPackage 的匹配项。

Here is a the basic steps for upgrading your security protocols:以下是升级安全协议的基本步骤:

Run both cmdlets to set .NET Framework strong cryptography registry keys.运行这两个 cmdlet 以设置 .NET Framework 强加密注册表项。 After that, restart PowerShell and check if the security protocol TLS 1.2 is added.之后,重新启动 PowerShell 并检查是否添加了安全协议 TLS 1.2。 As of last, install the PowerShellGet module.最后,安装 PowerShellGet 模块。

The first cmdlet is to set strong cryptography on 64 bit .Net Framework (version 4 and above).第一个 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM