3

I have read many forums and tried a lot of things, but I simply cannot add the PS gallery I am behind a corporate proxy, but I have setup my profile to use it. I'm trying to register the PS repository using these commands

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
[system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy('http://myproxy:1234')
[system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true
Register-PSRepository -default -Proxy http://myproxy:123
Get-PSRepository

Get-PSRepository is always returning a warning WARNING: Unable to find module repositories

I am on windows Server 2012 R2. I have verified the proxy has internet access by downloading an external URL so its definitely not an issue with the proxy being incorrect

How can I register the PS Gallery? Its driving me insane!

Thanks PS: Powershell version

Name                           Value
----                           -----
PSVersion                      5.1.14409.1018
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1018
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

UPDATE

I found that our corporate proxy is blocking requests to powershellgallery, this would explain a good chunk of the issues I'm facing. The proxy allows other http traffic but somehow blocks powershellgallery. I'll get in touch with the networking team and update this thread once I find out more.

NullPointer
  • 2,084
  • 7
  • 24
  • 38

2 Answers2

11

I got some "inspiration" from this post Invalid Web Uri error on Register-PSRepository

Came up with a brute force method which might help someone in the future

  1. Create a folder in C:\Users<username>\AppData\Local\Microsoft\Windows\PowerShell named "PowershellGet". If it exists, go to next step
  2. Inside that folder, create a file PSRepositories.xml. If it already exists, take a backup. The contents of this file are:
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Collections.Specialized.OrderedDictionary</T>
      <T>System.Object</T>
    </TN>
    <DCT>
      <En>
        <S N="Key">PSGallery</S>
        <Obj N="Value" RefId="1">
          <TN RefId="1">
            <T>Microsoft.PowerShell.Commands.PSRepository</T>
            <T>System.Management.Automation.PSCustomObject</T>
            <T>System.Object</T>
          </TN>
          <MS>
            <S N="Name">PSGallery</S>
            <S N="SourceLocation">https://www.powershellgallery.com/api/v2/</S>
            <S N="PublishLocation">https://www.powershellgallery.com/api/v2/package/</S>
            <S N="ScriptSourceLocation">https://www.powershellgallery.com/api/v2/items/psscript/</S>
            <S N="ScriptPublishLocation">https://www.powershellgallery.com/api/v2/package</S>
            <B N="Trusted">true</B>
            <B N="Registered">true</B>
            <S N="InstallationPolicy">Trusted</S>
            <S N="PackageManagementProvider">NuGet</S>
            <Obj N="ProviderOptions" RefId="2">
              <TN RefId="2">
                <T>System.Collections.Hashtable</T>
                <T>System.Object</T>
              </TN>
              <DCT />
            </Obj>
          </MS>
        </Obj>
      </En>
    </DCT>
  </Obj>
</Objs>

I have directly added the locations for SourceLocation, PublishLocation etc

Done, now start a new PS window and type Get-PSRepository and it should show this repo.

NullPointer
  • 2,084
  • 7
  • 24
  • 38
  • 1
    My god, I owe you a beer... how is it even remotely possible that the PowerShell gallery api is still having certificate errors, I mean your s**t together Microsoft! – Dalbir Singh Nov 13 '21 at 02:42
1

I know this question is a bit old, but I'd like to add an additional step to "NullPointer"'s answer:

Reference: https://github.com/PowerShell/PowerShellGallery/issues/153

Run this command: Invoke-WebRequest 'https://www.powershellgallery.com/api/v2/'

If the error message's like "Invoke-WebRequest : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel." That means maybe the certificate is overwritten by the Proxy server (The middleman certificate)

Try typing below command to force the certificate to be trusted (temporarily)

add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

And try to install module or register again :)

User2012384
  • 4,769
  • 16
  • 70
  • 106