10

在 Windows 11 月更新(PackageManagementPowerShellGet1.0.0.1 版本的模块)之后,我无法再将 HTTPS NuGet 服务器注册为 PSRepository:

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

它返回错误:

# 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.
4

5 回答 5

13

就我而言,问题在于源位置的 (https) 服务器仅支持 TLS 1.2。

在 Windows 7 的 PowerShell 5.1 中运行,默认仅支持 SSL3 和 TLS 1.0。

以下允许它工作:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Register-PSRepository -Name "Artifactory" -SourceLocation "https://example.com/artifactory/api/nuget/powershell/"
于 2018-06-13T04:57:58.193 回答
11

这是由与访问 HTTPS 端点相关的错误引起的,该错误可能很快就会得到修复。

我仍然想分享OneGet 团队暗示的解决方法:

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'
    }
}

像使用普通一样使用它Register-PSRepository

Register-PSRepositoryFix -Name test -SourceLocation https://some-nuget/api/v2
于 2016-02-09T15:58:56.403 回答
2

感谢 Anton Purin,我将他的脚本更新为:

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

两个主要区别是:
1) Set-PSRepository -Name $Name -InstallationPolicy Untrusted 而不是 Set-PSRepository -Name PSGallery -InstallationPolicy Untrusted
2) 为 PowerShell 脚本设置 ScriptSourceLocation 和 ScriptPublishLocation

于 2017-03-09T11:31:39.587 回答
1

我有同样的问题,将powershell更新到5.1解决了这个问题。

于 2018-02-14T13:41:16.760 回答
1

该错误也是(错误地)由-Credential使用错误的用户名或密码传递引起的。

于 2021-07-27T17:20:11.133 回答