How to use NuGet package in PowerShell script?

I am writing a PowerShell script that uses the Mono.Cecil library. How do I install a package so that I can use it from a script? Thanks!

(For the record, I tried to figure out Googling, but all that came up was the result of PMC and Visual Studio, which is relevant to this issue.)

+10
source share
3 answers

Not finding a good solution, I just downloaded and unpacked the package manually through the NuGet API.

For those interested / others who have this problem, here is the code I used.

-nine
source

~ 5.x PowerShell version by default contains the source of the nuget package, but it does not work:

PS > Get-PackageSource Name ProviderName IsTrusted Location ---- ------------ --------- -------- nuget.org NuGet False https://api.nuget.org/v3/index.json PSGallery PowerShellGet False https://www.powershellgallery.com/api/v2/ 

If you are Unregister-PackageSource -Name nuget.org and Register-PackageSource -Location https://www.nuget.org/api/v2 -Name nuget.org -Trusted I was able to install nuget papckages using Install-Package from PowerShell, and not into a visual studio. Got an idea from this SO answer .

I don’t know what other possible negative consequences there are for removing the vgen nuget.org version of the v3 version, but I have been working this way for a while, and everything looks fine, your mileage may vary.

As an alternative, here is an example that does this work by pulling out nuget.exe, even if it's a shortcut way to do this:

 function Install-InvokeOracleSQL { $ModulePath = (Get-Module -ListAvailable InvokeSQL).ModuleBase Set-Location -Path $ModulePath if ($PSVersionTable.Platform -ne "Unix") { $SourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" $TargetNugetExe = ".\nuget.exe" Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe .\nuget.exe install Oracle.ManagedDataAccess Remove-Item -Path $TargetNugetExe } elseif ($PSVersionTable.Platform -eq "Unix") { nuget install Oracle.ManagedDataAccess.Core -Version 2.12.0-beta2 } } 
+6
source

I was able to install the package in PowerShell 6 (Core), indicating the source:

 PS > install-package gudusoft.gsqlparser -source https://www.nuget.org/api/v2 
+2
source

Source: https://habr.com/ru/post/1242546/


All Articles