Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to register PS Gallery

Tags:

powershell

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.

like image 868
NullPointer Avatar asked Oct 27 '25 10:10

NullPointer


1 Answers

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.

like image 92
NullPointer Avatar answered Oct 30 '25 00:10

NullPointer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!