Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Calling Register-PSRepository or Set-PSRepository triggers the error Unknown Option: -V in Powershell Core running on Ubuntu Linux #661

@tristanbarcelon

Description

@tristanbarcelon

Prerequisites

Steps to reproduce

  1. Create an Azure Devops YAML pipeline with following definition. To protect my private Nuget v2 feed, I've replaced the actual feed url with myorg and myfeed
trigger: none

pool:
   vmImage: 'ubuntu-latest'

variables:
- name: PowershellRepositoryName
  value: 'PrivatePSGallery'
- name: PowershellRepositorySourceLocation
  value: 'https://pkgs.dev.azure.com/**myorg**/_packaging/**myfeed**/nuget/v2'

- task: PowerShell@2
  displayName: 'Set or register PrivatePSGallery'
  inputs:
    targetType: 'filePath'
    filePath: '$(Build.SourcesDirectory)/src/setprivatepsgallery.ps1'
    arguments: '-RepositoryName $(PowershellRepositoryName) -RepositorySourceLocation $(PowershellRepositorySourceLocation)'
    failOnStderr: true
    showWarnings: true
    pwsh: true
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)
  1. define setprivatepsgallery.ps1 as follows and store in src folder:
[CmdletBinding()]
param(
    [Parameter(Mandatory = $true, HelpMessage = 'Powershell repository name to set or register')]
    [string] $RepositoryName,
    [Parameter(Mandatory = $true, HelpMessage = 'Azure Devops feed URL to use as repository source')]
    [string] $RepositorySourceLocation
)
begin {}
process {
    try
    {
        [string] $BuildDefinition = $ENV:BUILD_DEFINITIONNAME

        if ([string]::IsNullOrEmpty($ENV:SYSTEM_ACCESSTOKEN))
        {
            Write-Error "Build pipeline: $BuildDefinition needs to have access to SYSTEM_ACCESSTOKEN environment variable"
        }
        else
        {
            $AZDOCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @('buildagent', (ConvertTo-SecureString -String $ENV:SYSTEM_ACCESSTOKEN -AsPlainText -Force))
        }

        [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13

        Write-Host "Searching for repository: $($RepositoryName)"
        $PrivateRepository = Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue

        if ($null -eq $PrivateRepository)
        {
            $PrivateRepository = Get-PSRepository | Where-Object { ($_.SourceLocation -ieq $RepositorySourceLocation) -and ($_.Name -ine $RepositoryName) }
            if ($null -ne $PrivateRepository)
            {
                Write-Host "Unregistering repository $($RepositoryName)"
                Unregister-PSRepository -Name $RepositoryName
            }
            Write-Host "Registering repository $($RepositoryName) name with SourceLocation to $($RepositorySourceLocation)"
            Register-PSRepository -Name $RepositoryName -SourceLocation $RepositorySourceLocation -InstallationPolicy Trusted -Credential $AZDOCredential
        }
        else
        {
            if (($PrivateRepository.SourceLocation -ine $RepositorySourceLocation) -or ($PrivateRepository.InstallationPolicy -ine 'Trusted'))
            {
                Write-Host "Updating repository $($RepositoryName) because its InstallationPolicy and SourceLocation do not match expected values"
                Set-PSRepository -Name $RepositoryName -InstallationPolicy Trusted -SourceLocation $RepositorySourceLocation -Credential $AZDOCredential
            }
            else
            {
                Write-Host "Found repository: $($RepositoryName) and no updates are needed"
            }
        }
    }
    catch {
        throw $_
    }
    finally {
        Get-Variable -Scope local -ErrorAction SilentlyContinue | Remove-Variable -ErrorAction SilentlyContinue
    }
}
end {}
  1. Define a build pipeline using the yaml definition and execute it.

Somebody also mentioned in a PowershellGetV2 issue that they also see the "Unknown option: -V" error.

Azure Devops team is also able to repro the error on hosted agents and I'm able to repro it on my private Azure devops agents. I filed an Azure Devops issue initially, thinking this was a problem with the PowershellV2 task. I have also confirmed that pwsh is not installed as a dotnet global tool. pwsh was installed on private Azure Devops agents via apt package manager

For a mininum repro, reduce the script down to just this and execute in Ubuntu 18.04.5 or 20.04.*, just as long as myorg and myfeed are replaced with a valid Azure Devops nuget v2 feed and a valid Azure Devops personal access token with scope to read the nuget feed.

[string] $RepositoryName = 'PrivatePSGallery'
[string] $RepositorySourceLocation = 'https://pkgs.dev.azure.com/myorg/_packaging/myfeed/nuget/v2'
$AZDOCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList @('vstsagent', (ConvertTo-SecureString -String <replace this with your azure devops personal access token> -AsPlainText -Force))

Register-PSRepository -Name $RepositoryName -SourceLocation $RepositorySourceLocation -InstallationPolicy Trusted -Credential $AZDOCredential

Expected behavior

I expect it to create or update a repository named PrivatePSGallery on Ubuntu machines 18.04.* or 20.04.* using Powershell Core 7.1.x, 7.0.x releases.

Actual behavior

Searching for repository: PrivatePSGallery
Registering repository PrivatePSGallery name with SourceLocation to https://pkgs.dev.azure.com/**myorg**/_packaging/**myfeed**/nuget/v2
Unknown option: -V
.NET SDK (5.0.400)
Usage: dotnet [runtime-options] [path-to-application] [arguments]

Error details

I get an error like this:


Searching for repository: PrivatePSGallery
Registering repository PrivatePSGallery name with SourceLocation to https://pkgs.dev.azure.com/**myorg**/_packaging/**myfeed**/nuget/v2
Unknown option: -V
.NET SDK (5.0.400)
Usage: dotnet [runtime-options] [path-to-application] [arguments]

Execute a .NET Core application.

runtime-options:
  --additionalprobingpath <path>   Path containing probing policy and assemblies to probe for.
  --additional-deps <path>         Path to additional deps.json file.
  --fx-version <version>           Version of the installed Shared Framework to use to run the application.
  --roll-forward <setting>         Roll forward to framework version  (LatestPatch, Minor, LatestMinor, Major, LatestMajor, Disable).

path-to-application:
  The path to an application .dll file to execute.

Usage: dotnet [sdk-options] [command] [command-options] [arguments]

Execute a .NET Core SDK command.

sdk-options:
  -d|--diagnostics  Enable diagnostic output.
  -h|--help         Show command line help.
  --info            Display .NET Core information.
  --list-runtimes   Display the installed runtimes.
  --list-sdks       Display the installed SDKs.
  --version         Display .NET Core SDK version in use.

SDK commands:
  add               Add a package or reference to a .NET project.
  build             Build a .NET project.
  build-server      Interact with servers started by a build.
  clean             Clean build outputs of a .NET project.
  help              Show command line help.
  list              List project references of a .NET project.
  msbuild           Run Microsoft Build Engine (MSBuild) commands.
  new               Create a new .NET project or file.
  nuget             Provides additional NuGet commands.
  pack              Create a NuGet package.
  publish           Publish a .NET project for deployment.
  remove            Remove a package or reference from a .NET project.
  restore           Restore dependencies specified in a .NET project.
  run               Build and run a .NET project output.
  sln               Modify Visual Studio solution files.
  store             Store the specified assemblies in the runtime package store.
  test              Run unit tests using the test runner specified in a .NET project.
  tool              Install or manage tools that extend the .NET experience.
  vstest            Run Microsoft Test Engine (VSTest) commands.

Additional commands from bundled tools:
  dev-certs         Create and manage development certificates.
  fsi               Start F# Interactive / execute F# scripts.
  sql-cache         SQL Server cache command-line tools.
  user-secrets      Manage development user secrets.
  watch             Start a file watcher that runs a command when files change.

Run 'dotnet [command] --help' for more information on a command.

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.4
PSEdition                      Core
GitCommitId                    7.1.4
OS                             Linux 4.15.0-154-generic PowerShell/PowerShell#161-Ubuntu SMP Fri Jul 30 13:04:17 UTC 2021
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions