Unable to script installation of NuGet Package Provider in Powershell - powershell

I am trying to install the NuGet Package Provider using this command.
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
I've also tried adding the -Confirm:$false flag. I continue to be prompted for confirmation.
For example,
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\Booker\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"):
I want to complete this installation in a script. How can I bypass the confirmation prompt?

This works to get the Nuget Package Provider installed without confirmation.
Find-PackageProvider -Name 'Nuget' -ForceBootstrap -IncludeDependencies

Related

what is the correct way of installing Google.Apis.Calendar.v3 using PowerShell

Using PowerShell v7.3.1, I am trying to install the Google.Apis.Calendar.v3 library, but I get this error.
As suggested I run Get-PackageSource
I have also installed .NET Framework 4.8.1
I have also tried Install-Module PowerShellGet -Force, which successfully installed the module, but then when I try Install-Package Google.Apis.Calendar.v3 I get the same error.
what is the correct way of installing Google.Apis.Calendar.v3 using PowerShell?
Google.Apis.Calendar.v3 is a Google package. Which is hosted on NuGet
You don't appear to have NuGet set as a package source See: register-packagesource
register-PackageSource -Name MyNuGet -Location https://www.nuget.org/api/v2 -ProviderName NuGet

How to use Powershell's Uninstall-Package to uninstall *a* specific package from *a* specific csproject

So I'm trying to automate some workflow and I'm running into issues with uninstalling/reinstalling a specific package on a specific csproject. I know this is possible with Package-Manager, but how would I achieve this within a powershell script? Here's what my script looks like so far:
# delete package folder from local feed
Remove-Item 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\<packageFolder>' -Recurse
# delete past package
Remove-Item '.\<package>.nupkg'
# pack new package
nuget pack '<nuspecPath>.nuspec' -NoDefaultExcludes
# push package to local feed
nuget add '.\<package>.nupkg' -Source 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages'
**# Everything below this is not currently working #**
# Remove project's outdated package
Uninstall-Package -Name '<packageName>' -Project '<pathToProject>' -Force
# Install new package to project
Install-Package -Name '<packageName>' -Project '<pathToProject>' -Source 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages' -Force
So, apparently -Project or -ProjectName are not recognized atrributes when using Uninstall-Package in powershell.. So, how do I specify powershell to uninstall a specific package on a specific csproject? Any help or insights would be much appreciated!
I tried the above code and kept getting complaints about -ProjectName attribute. I thought that this command would uninstall a nuget package from a specific project and then reinstall a new version of that package to the project.
The following answer is valid for .NET (Core) SDK-style projects, but does not work for "classic" .NET Framework projects (see this comment):
You may use the dotnet CLI to manage your projects and solutions.
Run the following command to list all packages:
dotnet list <PATH-TO-PROJECT> package
Run the following command to remove a package from the project:
dotnet remove <PATH-TO-PROJECT> package <PACKAGE-NAME>
See Install and manage NuGet packages with the dotnet CLI for more information.

'No match was found ..' when installing a module with -MinimumVersion

I believe I might be misunderstanding the -MinimumVersion parameter of Install-Module
The help states
Specifies the minimum version of a single module to install. The version installed must be greater than or equal to MinimumVersion. If there is a newer version of the module available, the newer version is installed.
However, with the following command
Install-Module -Name RunAsUser -MinimumVersion 2.3.1
I receive this error
PackageManagement\Find-Package : No match was found for the specified search criteria and module name 'RunAsUser'.
Try Get-PSRepository to see all available registered module repositories.
If I instead run
Install-Module -Name RunAsUser -RequiredVersion 2.3.1
It will install version 2.3.1 successfully. If I run
Install-Module -Name RunAsUser
It will install the latest which is 2.4.0 Am I misusing the -MinimumVersion parameter?
Powershell version 5.1.19041.2364
I got this error trying to update PowershellGet with -Scope CurrentUser
PackageManagement\Install-Package : The following commands are already available on this
system:'Find-Package,Install-Package,Uninstall-Package'. This module 'PackageManagement' may override the existing
commands. If you still want to install this module 'PackageManagement', use -AllowClobber parameter.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
Seemed like all these issues related to 1.0.0.1 so I simply went into C:\Program Files\WindowsPowershell\Modules\PowerhsellGet and moved the folder 1.0.0.1 out of there. Now everything is working as expected.

NuGet dependency loop detected installing Microsoft.Data.Tools.Msbuild

I need to install the Microsoft.Data.Tools.Msbuild package. Is there anything I can do about this?
I used the command specified at https://www.nuget.org/packages/Microsoft.Data.Tools.Msbuild, but it should specify -RequiredVersion.
PS C:\> Install-Package Microsoft.Data.Tools.Msbuild -RequiredVersion 16.0.62004.28040 -Scope CurrentUser -ProviderName 'NuGet' -Source 'https://api.nuget.org/v3/index.json'
The package(s) come(s) from a package source that is not marked as trusted.
Are you sure you want to install software from 'nuget.org'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
Install-Package: Dependency loop detected for package 'Microsoft.Data.Tools.Msbuild'.

Unable to install NuGet package provider in PowerShell Core on Linux

I'm trying to set up the NuGet package provider on Linux in PowerShell 7 so I can useInstall-Package to get a package from the NuGet Gallery. However, when I run:
Install-PackageProvider -Name NuGet -Force
I get the following error:
Install-PackageProvider: No match was found for the specified search criteria for the provider 'NuGet'.
The package provider requires 'PackageManagement' and 'Provider' tags. Please check if the specified
package has the tags.
I did a bit of searching and found a few questions from this site and others where this error occurs, some answers saying I need to force TLS 1.2:
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
some saying to specify -RequiredVersion on Install-PackageSource, some saying to use-ForceBootstrap, and some saying to use -Force. None of these work and I'm still met with the same error each time. Get-PackageProvider lists NuGet as a provider.
I'm was also unable to install the NuGet provider on Windows using PowerShell Core with the same error. Is this just not supported from PowerShell Core?
I also received this error and specifying the version (currently 3.0.0.1) also fails. What worked for me was piping the packageprovider into Install-PackageProvider
Get-PackageProvider | where name -eq 'nuget' | Install-PackageProvider
You may add -Force if you want to avoid answering yes to The package(s) come(s) from a package source that is not marked as trusted. Are you sure you want to install software from ''?
The NuGet location has been updated. You can also do it in one line:
Register-PackageSource -Name nuget.org -ProviderName NuGet -Location "https://api.nuget.org/v3/index.json" -Trusted
Having TLS12 set in conjunction with Install Nuget package worked for me.
Below is how I used the command:
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet
In addition to #DougMaurer's answer, I also had to configure the package source as well:
$sourceArgs = #{
Name = 'nuget.org'
Location = 'https://api.nuget.org/v3/index.json'
ProviderName = 'NuGet'
}
Register-PackageSource #sourceArgs