Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
like image 569
Anton Avatar asked Feb 09 '16 15:02

Anton


4 Answers

That error is also (erroneously) caused by passing -Credential with the wrong username or password.

like image 92
Vimes Avatar answered Sep 27 '22 20:09

Vimes


In my case, the problem was that the (https) server at the source location only supported TLS 1.2.

Running on Windows 7 in PowerShell 5.1, the default was to support only SSL3 and TLS 1.0.

The following allowed it to work:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Register-PSRepository -Name "Artifactory" -SourceLocation "https://example.com/artifactory/api/nuget/powershell/"
like image 13
Mike Schenk Avatar answered Oct 23 '22 08:10

Mike Schenk


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
like image 11
Anton Avatar answered Oct 23 '22 08:10

Anton


Thanks to Anton Purin I've updated his script to this:

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 = (New-Object -TypeName Uri -ArgumentList $SourceLocation, 'items/psscript/').AbsoluteUri
    $repos[$Name].ScriptPublishLocation = (New-Object -TypeName Uri -ArgumentList $SourceLocation, 'package/').AbsoluteUri
    $repos | Export-Clixml -Path $PSRepositoriesXmlPath

    # Reloading PSRepository list
    Set-PSRepository -Name $Name -InstallationPolicy Untrusted
    Write-Verbose 'Registered via workaround'
}
}
# Usage Example
Register-PSRepositoryFix -Name "Name" -SourceLocation "http://address:port/api/v2/" -Verbose

Two main differences are:
1) Set-PSRepository -Name $Name -InstallationPolicy Untrusted instead of Set-PSRepository -Name PSGallery -InstallationPolicy Untrusted
2) Setting ScriptSourceLocation and ScriptPublishLocation for PowerShell scripts

like image 2
Vladimir Titkov Avatar answered Oct 23 '22 09:10

Vladimir Titkov