16

I want to add a nuget package feed that is hosted on our TFS instance to all of our developer's workstations. The problem that I have is that if the source has already been added, I get an error stating The name specified has already been added to the list of available package sources. Please provide a unique name.

What I want to do is check if a nuget source has already been registered on the machine before I run the code to add the source. Checking the documentation for nuget.exe I tried to use the List operation along with the Name and Source but I just get the same result as if I just run nuget sources

All of these commands:

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

Return the same result:

Registered Sources:

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

I am using Powershell to run these commands and came up with a workaround, but ideally I am hoping there is a nuget.exe command line option that will get this info for me.

3 Answers 3

15

In PowerShell v5, you have access to the PackageManagement module. This includes a NuGet package provider:

$nuget = Get-PackageProvider -Name NuGet

Alongside this, you can access all your sources:

$nuget | Get-PackageSource

By default, this will only have nuget.org, but with your added source(s), you will see them from the result of this command as well. As a bonus, because it's a powershell command, it returns objects instead of strings, so you can do the following:

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

To address your Q&A:

if (-not $(Get-PackageSource -Name myPowershellFeed -ProviderName NuGet -ErrorAction Ignore))
{
    # add the packagesource
2
  • Beautiful, that is exactly what I was looking for! A new module to learn about. It looks like I can use the Register-PackageSource instead of fiddling around with nuget.exe as well! Oct 16, 2018 at 21:17
  • @BrandonMcClure Yeah, the v5 stuff isn't well-known yet. The module also include MSU and MSI package providers for those kind of files as well. Super neat stuff. Oct 16, 2018 at 21:19
3

You can use the follow line:

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

Or even encapsulate it in a function:

function HasNugetSource ($url){
    return !!(nuget source | ? { $_ -like "*$url"});
}
2
  • 2
    Your double ! does nothing, FYI. Unless you're trying to cast to bool in which case.. just cast it to [bool] Oct 16, 2018 at 21:11
  • This solution was useful to me, but taking into account @MaximilianBurszley 's comment. I used it like this: [bool](nuget source | ? { $_ -like "*$url"}) Sep 24, 2021 at 23:05
2

This is a simple Windows cmd line to add a new nuget source if it hasn't been added before (in this case source name is "test"):

nuget sources | findstr test || (nuget sources Add -Name test -Source "https://some-nuget-source")

So what it does, it tries to find string "test" in listed nuget sources, if it finds it, it does nothing, otherwise it proceeds with adding a new source.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.