如何在PowerShell脚本中使用NuGet包?

时间:2021-07-26 00:32:12

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!

我正在编写一个使用Mono.Cecil库的PowerShell脚本。我如何安装软件包以便在脚本中使用它?谢谢!

(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.)

(为了记录,我在问这个之前确实尝试过谷歌搜索,但所有出现的结果都是关于PMC和Visual Studio的结果,这与这个问题无关。)

2 个解决方案

#1


5  

~5.x versions of PowerShell have a nuget package source included by default but it doesn't work:

~5.x版本的PowerShell默认包含一个nuget包源,但它不起作用:

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 -Name 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.

如果您取消注册 - PackageSource -Name nuget.org和Register-PackageSource -Location https://www.nuget.org/api/v2 -name nuget.org -Trusted我已经能够安装nuget papckages,只需安装Install-Package PowerShell,不在visual studio中。从这个SO答案中得到了想法。

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.

我不知道除去nuget.org源码的v3版本还有什么其他可能的负面影响,但我已经运行了一段时间,看起来没事,你的里程可能会有所不同。

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:

作为替代方案,这里是一个通过拉下nuget.exe来完成工作的例子,即使它是一个非常糟糕的方式来执行此操作:

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
    }
}

#2


-5  

Never mind, 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.

没关系,我最后只是通过NuGet API手动下载和解压缩包。对于那些有兴趣/有其他问题的人,这里是我使用的代码。

#1


5  

~5.x versions of PowerShell have a nuget package source included by default but it doesn't work:

~5.x版本的PowerShell默认包含一个nuget包源,但它不起作用:

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 -Name 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.

如果您取消注册 - PackageSource -Name nuget.org和Register-PackageSource -Location https://www.nuget.org/api/v2 -name nuget.org -Trusted我已经能够安装nuget papckages,只需安装Install-Package PowerShell,不在visual studio中。从这个SO答案中得到了想法。

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.

我不知道除去nuget.org源码的v3版本还有什么其他可能的负面影响,但我已经运行了一段时间,看起来没事,你的里程可能会有所不同。

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:

作为替代方案,这里是一个通过拉下nuget.exe来完成工作的例子,即使它是一个非常糟糕的方式来执行此操作:

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
    }
}

#2


-5  

Never mind, 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.

没关系,我最后只是通过NuGet API手动下载和解压缩包。对于那些有兴趣/有其他问题的人,这里是我使用的代码。