点击
搜索 / Search

最近在看 PowerShell 的书,毕竟初学还是看点能看懂的东西(指中文)比较好。唯一的缺点就是书有点老,实验做的不畅快,所以就有了这篇文。

书后练习提到使用Install-Module为本机系统安装更多的 PSModule,不过我照着运行之后却提示出错:

PS C:\Users\user> Install-Module -Name PSWindowsUpdate
PackageManagement\Install-Package : 找不到与指定的搜索条件和程序包名称“PSWindowsUpdate”匹配的项目。请尝试使用 Get-PSRepository 查看所有可用的注册程序包源。
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1772 char:21
+ ...          $null = PackageManagement\Install-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
    + FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

PS C:\Users\user> PackageManagement\Install-Package : 找不到与指定的搜索条件和程序包名称“PSWindowsUpdate”匹配的项目。请尝试使用 Get-PSRepository 查看所有可用的注册程序包源。

机器是 Windows 7,PowerShell 已经升级到 5.1。解决问题的第一步当然是按软件自身的提示来检查,所以先运行了Get-PSRepository

PS C:\Users\user> Get-PSRepository
WARNING: 找不到程序包源。

想到是不是因为安装源的问题,所以运行了Register-PSRespository -default,但没有反应。所以只好求助万能的因特网了。最后找到的解决方法是这样:

//为旧版本的 PowerShell 启用 TLS 1.2 支持
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
//重新注册默认 PSGallery 存储库
Register-PSRepository -Default -Verbose
//设置信任安装源
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted

检查一下:

PS C:\Users\user> Get-PSRepository

Name                      InstallationPolicy   SourceLocation
----                      ------------------   --------------
PSGallery                 Trusted              https://www.powershellgallery.com/api/v2

再运行Install-ModuleUpdate-Module就不会报错了。这件事告诉我们 outdated 的系统还是挺恐怖的。

参考资料:

  1. https://devblogs.microsoft.com/powershell/powershell-gallery-tls-support/
  2. https://dev.to/darksmile92/powershell-disabled-support-for-tls-1-0-for-the-gallery-update-module-and-install-module-broken-1oii
声明: 本站所有图文遵循 署名-非商业性使用-相同方式共享 3.0 共享 协议.
转载请注明转自 ershiwo的私人研究所
标签:

2 Comments

  1. 执行下面命令时非常慢:
    Register-PSRepository -Default -Verbose
    一度怀疑存在问题,不过最终还是成功了。
    非常感谢🙏

    2022.12.05 at 01:28
    #1  
    • 可能最好的办法还是换新系统吧 :em01: 总之能帮到你就好。

      2022.12.05 at 17:27
      #11