How to Use a Nuget Package Within a Powershell Script

How can I deploy a PS1 script with a nuget package

When a project using packages.config installs a NuGet package, the package's tools\install.ps1 script will run. However, this no longer happens when the project using the package uses PackageReference (such as SDK style projects, used by .NET Core).

Similarly, the files in the content folder of the nupkg are copied into the project on install, but only when the project uses packages.config. PackageReference projects use the contentFiles folder in the nupkg, however the behaviour is different. Those files are copied only on build, not install, for .NET Framework projects and on publish for .NET Core projects. Probably not what you want for signing assemblies.

The feature you probably want to use is including MSBuild props and targets in your package. Note that the props and targets file names must match the package id exactly for NuGet to use them. You probably want to use afterTargets="build" at a guess.

Publising nuget package with Powershell scripts - scripts are not packaged

If you package project file directly, you need to change that file’s Build Action to Content. (Right click the file in VS > Properties >Change Build Action to Content)

If you just create .nuspec file in a folder (no project file, such as .csproj) and pack this .nuspec file, you can refer to these steps:

  1. Add Nuget.exe tool to source control and map to build agent (same folder level (content folder) that you need to package) or add it to the environment variable (path) of build agent service account.
  2. Add Command Line task (Tool: nugget.exe; Arguments: spec; Working folder:[same level path that you need to package, such as the folder path that contains content folder]
  3. Add NuGet Packager task (Path to csproj or nuspec files to pack: **\*.nuspec)

How can I run Nuget PowerShell cmdlet Install-Package in normal PowerShell session (not inside Visual Studio)?

I would go with *-Package cmdlets. I've done some automation stuff with NuGet, where I had to choose between NuGet CLI or PowerShell *-Package cmdlets. Since nuget list --allversion doesn't work since Feb 2017 (see GitHub ticket). I decided to use the PowerShell cmdlets.

From PowerShell docu:

PS> Install-Package -Name NuGet.Core -Source MyNuGet -Credential Contoso\TestUser

You may have to define the provider name to NuGet via -ProviderName:

Specifies one or more package provider names to which to scope your package search. You can get package provider names by running the Get-PackageProvider cmdlet.

...

Accepted values: msi, Programs, msu, Bootstrap, PSModule, nuget, chocolatey

Reason why I wrote this answer though this question is marked as duplicate:

The stackoverflow answer marking this question as duplicate, refers to either use Visual Studios built in management console (which is implemented as seperate Powershell host, and therefore not useable via the standard PowerShell) or via the NuGet CLI tools (which didn't cover all my automation requirements). Using "*-Package` cmdlets are not covered by this answer.

UPDATE 1

Here is an example how to install log4Net NuGet package:

  Install-Package log4net -ProviderName NuGet -Source https://www.nuget.org/api/v2 -Scope CurrentUser -Force

The get more information what is installed in the background you can attach the -Verbose switch.

As far as I can remember you've to use the v2 API of NuGet.

How to create a powershell script that triggers a NuGet Update-Package –reinstall?

You should run the update command from nuget.exe. One of the parameters of the update command is FileConflictAction, which tells what action to take when asked to overwrite or ignore existing files referenced by the project: overwrite, Ignore, None.

You might have to wrap everything in a powershell script, possibly referring to EnvDTE, to set your paths and to mimick the environment you have from the PowerShell console within Visual Studio.

Execute powershell scripts through nuget command line

It is not supported by NuGet.exe. There is a discussion on codeplex about this and there are some issues, which have been closed, that were raised in their issue tracking system. There is also a blog post by David Ebbo about the problems with supporting this feature.

The general problem is that within a PowerShell script you can use the Visual Studio object model so to be able to expose this from outside of Visual Studio and support all scenarios is non-trivial.

I looked at supporting the running of PowerShell scripts from outside Visual Studio by using SharpDevelop as an experiment to see what was possible. It is quite a heavyweight solution since it requires SharpDevelop to work and not just NuGet.exe.

Using Nuget package feed from PowerShell in Azure pipeline

I can reproduce your issue:

Sample Image

How I solve this:

$patToken = "<Your PAT here>" | ConvertTo-SecureString -AsPlainText -Force

$credsAzureDevopsServices = New-Object System.Management.Automation.PSCredential("<Your email address that related to PAT here>", $patToken)

Register-PackageSource -Name "xxx" -Location "https://pkgs.dev.azure.com/<organizaton name>/_packaging/<feed name>/nuget/v2" -ProviderName NuGet -Credential $credsAzureDevopsServices

Please refer to these official documents:

https://docs.microsoft.com/en-us/powershell/module/packagemanagement/register-packagesource?view=powershell-7.2

https://docs.microsoft.com/en-us/azure/devops/artifacts/tutorials/private-powershell-library?view=azure-devops#connecting-to-the-feed-as-a-powershell-repo

Sample Image

Let me know if you have more concerns.



Related Topics



Leave a reply



Submit