12

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.

6 Answers 6

13

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

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
7
  • 3
    This is incredible. Who doesn't test with, let alone actually use, https endpoints? Feb 11, 2016 at 20:51
  • Maybe error is more specific rather than just HTTPS NuGet endpoint, I couldn't debug C# module.
    – Anton
    Mar 21, 2016 at 14:48
  • This is fixed in the anniversary update, so this workaround should no longer be necessary. Aug 8, 2016 at 15:09
  • @RitchMelton Is it fixed on Win 7 though?
    – tidmutt
    Apr 14, 2018 at 16:22
  • 1
    @tidmutt Maybe? I've ran into other problems with Win7 powershell where built-in packages on newer systems (cryptography cmdlets for instance) that I just wouldn't bother with Win 7 at all. Apr 20, 2018 at 15:47
3

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

2

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

1
  • 1
    Exactly this, very confusing... Jan 20, 2022 at 7:08
1

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

0

I had this problem. I needed to allow network traffic for ieexec.exe (and PowerShell ofcourse) on firewall. This helps me solve this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.