Invalid Web Uri error on Register-PSRepository

﹥>﹥吖頭↗ 提交于 2019-12-04 11:42:46

问题


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.

回答1:


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



回答2:


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/"



回答3:


I had the same issue, updating powershell to 5.1 resolved the problem.




回答4:


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



来源:https://stackoverflow.com/questions/35296482/invalid-web-uri-error-on-register-psrepository

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!