簡體   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