29

I'm writing a PowerShell script that makes use of the Mono.Cecil library. How would I install the package so I can use it from within the script? Thanks!

(For the record, I did try Googling before asking this, but all that came up was results about PMC and Visual Studio, which aren't relevant to this question.)

0

3 Answers 3

19

I was able to install a package in PowerShell 6 (Core) by specifying the source:

PS > install-package gudusoft.gsqlparser -source https://www.nuget.org/api/v2
Sign up to request clarification or add additional context in comments.

1 Comment

If this command appears to hang, it's due to an infinite loop (bug) getting dependencies. Run with -SkipDependencies to work around this.
10

~5.x versions of PowerShell have a nuget package source included by default but it doesn't 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 Unregister-PackageSource -Source nuget.org and Register-PackageSource -Location https://www.nuget.org/api/v2 -name nuget.org -Trusted I have been able to install nuget papckages with just Install-Package from PowerShell, not within visual studio. Got the idea from this SO answer.

I don't know what other possible negative impacts there are to removing the v3 version of the nuget.org source but I have been running this way for a while and things appear ok, your mileage may vary.

As an alternative here is an example that gets the job done by pulling down the nuget.exe even if it is a crummy way to have 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
    }
}

3 Comments

Unregister-PackageSource -Source nuget.org
@JoshGust Name appears to be aliased as Unregister-PackageSource -Name nuget.org -WhatIf seems to work but -Source is more correct so I have edited the answer.
I found that Register-PackageSource nugetV2 https://www.nuget.org/api/v2 -ProviderName NuGet followed by install-package packageName -Source nugetV2 got me there without having to unregister anything. @JamesKo I did have to manually do add-type -path longPathName - I wasn't sure how much of this you expected to be automatic. Get-Package will show you where it got installed (and it does get unpacked automatically). And if you want to run without admin privs include -Scope CurrentUser
-22

Unable to find a good solution, I ended up just downloading and unzipping the package manually via the NuGet API.

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

3 Comments

"I gave up" doesn't really qualify as an answer, and certainly not as the accepted answer. At best it belongs as a comment, so that the question appears to remain unanswered (and can be found by people who might know what to do).
@Tullo_x86: the accepted mark is entirely up to the question asker, always. This does qualify as an answer, because it attempts to suggest a solution. We can use voting to indicate how helpful we think it is as an answer.
Years later in 2025, this definitely should not be an accepted answer as the link is now dead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.