检查是否已配置Nuget源

时间:2018-10-16 20:02:59

标签: powershell nuget

我想将托管在我们的TFS实例上的nuget软件包提要添加到我们所有开发人员的工作站。我的问题是,如果已经添加了源,则会收到一条错误消息,指出 The name specified has already been added to the list of available package sources. Please provide a unique name.

我想要做的是在运行添加源的代码之前检查nuget源是否已在计算机上注册。检查documentation for nuget.exe时,我尝试将ListName一起使用Source操作,但得到的结果与运行nuget sources的结果相同。 / p>

所有这些命令:

nuget sources list -Source $myURL
nuget sources list -Name $myName
nuget sources

返回相同的结果:

Registered Sources:

  1.  nuget.org [Enabled]
      https://api.nuget.org/v3/index.json
  2.  myPowershellFeed [Enabled]
    https://myURL.myDomain.org

我正在使用Powershell运行这些命令并提出了解决方法,但是理想情况下,我希望有一个nuget.exe命令行选项可以为我获取此信息。

3 个答案:

答案 0 :(得分:3)

在PowerShell v5中,您可以访问PackageManagement模块。其中包括NuGet软件包提供程序:

$nuget = Get-PackageProvider -Name NuGet

除此之外,您还可以访问所有资源:

$nuget | Get-PackageSource

默认情况下,它仅具有nuget.org,但是在添加了源的情况下,您也将从此命令的结果中看到它们。另外,由于它是powershell命令,因此它返回对象而不是字符串,因此您可以执行以下操作:

Get-PackageSource -Name myPowershellFeed |
    Format-List -Property * -Force

要解决您的问答:

if (-not $(Get-PackageSource -Name myPowershellFeed -ProviderName NuGet -ErrorAction Ignore))
{
    # add the packagesource

答案 1 :(得分:0)

我在Powershell中的解决方法是:

if([string]::IsNullOrEmpty({nuget sources | where {$_ -like "*$myURL*"}})){
    #DoWork
}
else{
    #Take a break, it already exists
}

答案 2 :(得分:0)

您可以使用以下行:

$nugetHasMyUrlSource =!!(nuget source | ? { $_ -like "*$myUrl"})

或者甚至将其封装在一个函数中

function HasNugetSource ($url){
    return !!(nuget source | ? { $_ -like "*$url"});
}
相关问题