Search code examples
powershellonegetpowershellget

Invalid Web Uri error on Register-PSRepository


After Windows November update (PackageManagement and PowerShellGet modules of 1.0.0.1 version) I can't register HTTPS NuGet servers as PSRepository anymore:

Register-PSRepository -Name test -SourceLocation https://some-nuget/api/v2

It returns error:

# Register-PSRepository : The specified Uri 'https://some-nuget/api/v2' for parameter 'SourceLocation' is an invalid Web Uri. Please ensure that it meets the Web Uri requirements.

Solution

  • This is caused with a bug related to accessing HTTPS endpoints which probably will be fixed soon.

    I still want to share a workaround kindly hinted by OneGet team:

    Function Register-PSRepositoryFix {
        [CmdletBinding()]
        Param (
            [Parameter(Mandatory=$true)]
            [String]
            $Name,
    
            [Parameter(Mandatory=$true)]
            [Uri]
            $SourceLocation,
    
            [ValidateSet('Trusted', 'Untrusted')]
            $InstallationPolicy = 'Trusted'
        )
    
        $ErrorActionPreference = 'Stop'
    
        Try {
            Write-Verbose 'Trying to register via ​Register-PSRepository'
            ​Register-PSRepository -Name $Name -SourceLocation $SourceLocation -InstallationPolicy $InstallationPolicy
            Write-Verbose 'Registered via Register-PSRepository'
        } Catch {
            Write-Verbose 'Register-PSRepository failed, registering via workaround'
    
            # Adding PSRepository directly to file
            Register-PSRepository -name $Name -SourceLocation $env:TEMP -InstallationPolicy $InstallationPolicy
            $PSRepositoriesXmlPath = "$env:LOCALAPPDATA\Microsoft\Windows\PowerShell\PowerShellGet\PSRepositories.xml"
            $repos = Import-Clixml -Path $PSRepositoriesXmlPath
            $repos[$Name].SourceLocation = $SourceLocation.AbsoluteUri
            $repos[$Name].PublishLocation = (New-Object -TypeName Uri -ArgumentList $SourceLocation, 'package/').AbsoluteUri
            $repos[$Name].ScriptSourceLocation = ''
            $repos[$Name].ScriptPublishLocation = ''
            $repos | Export-Clixml -Path $PSRepositoriesXmlPath
    
            # Reloading PSRepository list
            Set-PSRepository -Name PSGallery -InstallationPolicy Untrusted
            Write-Verbose 'Registered via workaround'
        }
    }
    

    Use it as you would use ordinary Register-PSRepository:

    Register-PSRepositoryFix -Name test -SourceLocation https://some-nuget/api/v2