Using Nuget package feed from PowerShell in Azure pipeline

I'm trying to write a pipeline PowerShell task that checks that package version numbers in the csproj files have been incremented -- so that the push step doesn't fail because of duplicates when building new release packages. (We use allowPackageConflicts: false on the NuGetCommand@2 command: push task.)

The problem is to get Find-Package to use my Azure DevOps NuGet feed that all the packages go to.

- task: PowerShell@2
  displayName: 'Check package version numbers'
    targetType: 'inline'
    script: |
      [bool] $bad = $false;

      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
      Register-PackageSource -Name TheAzureDevOpsFeed -Trusted -Force -ProviderName NuGet -Location ""
      foreach( $csproj in Get-ChildItem **\*.csproj ) {
          $m = Select-String -Path $csproj.FullName -Pattern '<VersionPrefix>([\d\.]+)</VersionPrefix>'
          $pkgName = [System.IO.Path]::GetFileNameWithoutExtension($csproj.FullName)
          echo $pkgName
          if( $m.Matches.Count -eq 1 )
              $csprojVersion = $m.Matches[0].Groups[1].Value
              $pkg = $null
              try {
                  $pkg = Find-Package $pkgName -Source TheAzureDevOpsFeed -ErrorAction Stop
              catch {
                  $pkg = $null

              if( $pkg -eq $null )
                  echo "  No existing package."
              $current = $pkg.Versions[0].OriginalVersion
              if( [System.Version]"$csprojVersion" -le [System.Version]"$current" )
                  echo "  REJECTED: current version is $current but csproj has version $csprojVersion."
                  $bad = $true
              else {
                echo "  $current -> $csprojVersion"
          else {
              echo "  skipped"
      if( $bad ) {
        exit 1
      exit 0

When the task runs the output is:

Name                             ProviderName     IsTrusted  Location                                                  
----                             ------------     ---------  --------                                                                 NuGet            False                       
PSGallery                        PowerShellGet    False                  
Register-PackageSource : Source Location 
'' is not valid.
At D:\a\_temp\62c6a3fb-93a6-4d4a-8cf5-ce5dc7d9dcbf.ps1:7 char:1
+ Register-PackageSource -Name TheAzureDevOpsFeed -Trusted -Force -Provi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (TheAzureDevOpsFeed:String) [Register-PackageSource], Exception
    + FullyQualifiedErrorId : SourceLocationNotValid,Microsoft.PowerShell.PackageManagement.Cmdlets.RegisterPackageSou 

But why is it 'not valid'? It is valid -- it works fine in VisualStudio.

Things I've tried:

  • single quotes, double quotes, no quotes,
  • [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12,
  • -Location

1 answer

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum