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'
  inputs:
    targetType: 'inline'
    script: |
      [bool] $bad = $false;

      Get-PackageSource
      [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
      Register-PackageSource -Name TheAzureDevOpsFeed -Trusted -Force -ProviderName NuGet -Location "https://pkgs.dev.azure.com/Organisation/_packaging/TheAzureDevOpsFeed/nuget/v3/index.json"
      Get-PackageSource
    
      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."
                  continue
              }
              $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.org                        NuGet            False      https://api.nuget.org/v3/index.json                       
PSGallery                        PowerShellGet    False      https://www.powershellgallery.com/api/v2                  
Register-PackageSource : Source Location 
'https://pkgs.dev.azure.com/Organisation/_packaging/TheAzureDevOpsFeed/nuget/v3/index.json' 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 
   rce

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 https://pkgs.dev.azure.com/.../v2

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