Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在PowerShell脚本中使用NuGet包?_C#_.net_Powershell_Nuget_Nuget Package - Fatal编程技术网

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

C# 如何在PowerShell脚本中使用NuGet包?,c#,.net,powershell,nuget,nuget-package,C#,.net,Powershell,Nuget,Nuget Package,我正在编写一个利用库的PowerShell脚本。如何安装软件包以便在脚本中使用它?谢谢 (作为记录,我在问这个问题之前确实尝试过谷歌搜索,但结果都是关于和Visual Studio的,这与这个问题无关。)由于找不到好的解决方案,我最终只是通过NuGet API手动下载并解压缩包 对于那些对此问题感兴趣的人/其他有此问题的人,我使用的代码是。~5.x版本的PowerShell默认包含nuget软件包源代码,但它不起作用: PS > Get-PackageSource Name

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


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

由于找不到好的解决方案,我最终只是通过NuGet API手动下载并解压缩包


对于那些对此问题感兴趣的人/其他有此问题的人,我使用的代码是。

~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/

如果您
取消注册PackageSource-Source nuget.org
注册PackageSource-Locationhttps://www.nuget.org/api/v2 -名称nuget.org-Trusted
我已经能够使用PowerShell中的
安装包安装nuget PapPackages,而不是在visual studio中。我是从他那里得到这个主意的

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

作为替代方案,这里有一个示例,通过下拉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
    }
}

通过指定源,我可以在PowerShell 6(Core)中安装程序包:

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

“我放弃了”并不是一个真正合格的答案,当然也不是公认的答案。充其量,它只是一个评论,因此这个问题似乎没有得到回答(并且可能会被知道该怎么做的人发现)。@Tullo_x86:被接受的分数总是完全取决于提问者。这确实可以作为一个答案,因为它试图提出一个解决方案。我们可以使用投票来表明我们认为这是一个多么有用的答案。
取消注册PackageSource
-Source
nuget.org
@JoshGust Name的别名似乎是
取消注册PackageSource-Name nuget.org-WhatIf
似乎有效,但
-Source
更正确,因此我编辑了答案。我发现
Register PackageSource nugetV2https://www.nuget.org/api/v2 -ProviderName NuGet
后跟
安装软件包
packageName
-Source nugetV2
让我无需注销任何东西就可以到达那里@JamesKo我确实需要手动添加type-path,我不确定您希望有多少是自动的<代码>获取软件包将向您显示它的安装位置(并且它会自动解压缩)。如果您想在没有管理员权限的情况下运行,请包括
-Scope CurrentUser