繁体   English   中英

通过 PowerShell 脚本安装 NuGet

[英]Install NuGet via PowerShell script

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

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

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

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

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

这是一个简短的 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 一起提供。 这篇文章给出了思路。

这似乎也做到了。 PS示例:

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

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

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

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

使用 PowerShell 但无需创建脚本:

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

上述解决方案都不适合我,我找到了一篇解释该问题的文章 系统上的安全协议已被弃用,因此会显示一条错误消息,指出找不到 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

暂无
暂无

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

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