无法注册 PS 图库

Nul*_*ter 3 powershell

我读过很多论坛并尝试了很多东西,但我根本无法添加我在公司代理后面的 PS 画廊,但我已经设置了我的个人资料来使用它。我正在尝试使用这些命令注册 PS 存储库

[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
Run Code Online (Sandbox Code Playgroud)

Get-PSRepository总是返回警告WARNING: Unable to find module repositories

我使用的是 Windows Server 2012 R2。我已经通过下载外部 URL 验证了代理可以访问互联网,因此绝对不是代理不正确的问题

如何注册 PS Gallery?它让我发疯!

谢谢PS:Powershell版本

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
Run Code Online (Sandbox Code Playgroud)

更新

我发现我们的公司代理正在阻止对 powershellgallery 的请求,这可以解释我面临的大部分问题。该代理允许其他 http 流量,但以某种方式阻止 powershellgallery。我将与网络团队联系,并在了解更多信息后更新此线程。

Nul*_*ter 11

我从这篇文章 Invalid Web Uri error on Register-PSRepository 中得到了一些“灵感”

想出了一种暴力方法,可能会帮助将来的人

  1. 在 C:\Users<用户名>\AppData\Local\Microsoft\Windows\PowerShell 中创建一个名为“PowershellGet”的文件夹。如果存在则进入下一步
  2. 在该文件夹中,创建一个文件 PSRepositories.xml。如果已经存在,请进行备份。该文件的内容是:
<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>
Run Code Online (Sandbox Code Playgroud)

我直接添加了 SourceLocation、PublishLocation 等位置

完成后,现在启动一个新的 PS 窗口并输入 Get-PSRepository,它应该显示此存储库。