简体   繁体   中英

Powershell Install Nuget package

I am trying to find out how I can download and install the NUnit package by using Power Shell script.

I tried Install-Package command but that if of course for the NUGet Package Manager Console. Any ideas?

You need the NuGet package provider to be able to find and download NUnit using Install-Package . The issue I think you're running into is that if you do not previously have the NuGet package provider installed/configured for PowerShell, the first time you run a command like Install-Package NUnit , it will prompt you if you want to install the NuGet package provider.

Example:

Find-Package NUnit
WARNING: The specified PackageManagement provider 'NuGet' is not available.
Find-Package : No match was found for the specified search criteria and package name 'NUnit'.
At line:1 char:1
+ Find-Package NUnit
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ObjectNotFound: (Microsoft.Power...ets.FindPackage:FindPackage) [Find-Package], Exception
+ FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage

But after I've configured the NuGet provider, it works just fine:

Find-Package -Name Nunit

Name                           Version          Source                         Summary                                                                                                              
----                           -------          ------                         -------                                                                                                              
NUnit                          3.2.0            nuget.org                      NUnit is a unit-testing framework for all .Net languages with a strong TDD focus.  

My answer is building upon @Negorath's. I believe they are missing some details:

> Get-PackageProvider -ListAvailable

Name                     Version          DynamicOptions
----                     -------          --------------
msi                      3.0.0.0          AdditionalArguments
msu                      3.0.0.0
NuGet                    2.8.5.208        Destination, ExcludeVersion, Scope, SkipDependencies, Headers, FilterOnTag, Contains, AllowPrereleaseVersions, ConfigFile, SkipValidate
PowerShellGet            1.0.0.1          PackageManagementProvider, Type, Scope, AllowClobber, SkipPublisherCheck, InstallUpdate, NoPathUpdate, Filter, Tag, Includes, DscResource, RoleCapability, Command, PublishLocation, ScriptSourceL... Programs                 3.0.0.0          IncludeWindowsInstaller, IncludeSystemComponent

As you can see I have NuGet. But if you don't you can install with:

> Register-PackageSource -Name NuGet -ProviderName PowerShellGet -Verbose
VERBOSE: Repository details, Name = 'PSGallery', Location = 'https://www.powershellgallery.com/api/v2/'; IsTrusted = 'False'; IsRegistered = 'True'.
VERBOSE: Performing the operation "Register Package Source." on target "Package Source 'NuGet' () in provider 'PowerShellGet'.".

I tried many things then this one helped me to fix this :

>Open Powershell (as Admin)
>[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
>Install-PackageProvider -Name NuGet

Then you can see 'nuget' folder in the location : C:\\Program Files\\PackageManagement\\ProviderAssemblies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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