完美解决“Failed in attempting to update the source: winget”故障

本文详细介绍如何解决winget不能安装和更新软件,文章还介绍了如何在使用代理速软件下载。

分类:

Windows 11

winget


更新 winget 源

在一次使用Winget更新Power Shell时遇到以下错误:

# 命令
PS C:\WINDOWS\system32> winget install --id Microsoft.PowerShell --source winget
# 返回的命令输出
Failed in attempting to update the source: winget
An unexpected error occurred while executing the command:
InternetOpenUrl() failed.
0x80072efd : unknown error

这是由于我们的上网环境不稳定,导致连接失败,我们可以将winget的源变更为国内中科大的更新源:https://mirrors.ustc.edu.cn/winget-source

下面我们来演示如何更新winget源。

查看当前winget源

# 命令
PS C:\WINDOWS\system32> winget source list
# 返回的命令输出
Name    Argument                                      Explicit
--------------------------------------------------------------
msstore https://storeedgefd.dsx.mp.microsoft.com/v9.0 false
winget  https://cdn.winget.microsoft.com/cache        false

命令输出显示winget源为微软默认源。

PS C:\WINDOWS\system32> 为 PowerShell 命令提示符

’>’ 符号后面的“winget source list”为命令

删除微软默认的winget源

# 命令
PS C:\WINDOWS\system32> winget source remove winget
# 返回的命令输出
Removing source: winget...
  ██████████████████████████████  100%
Done

添加中科大的winget源

# 命令
PS C:\WINDOWS\system32> winget source add winget https://mirrors.ustc.edu.cn/winget-source --trust-level trusted
# 返回的命令输出
Adding source:
  winget -> https://mirrors.ustc.edu.cn/winget-source
  ██████████████████████████████  100%
Done

添加中科大的源后就可以安装或更新软件了。

(可选操作)重置 winget 源为微软默认源

# 命令
PS C:\WINDOWS\system32> winget source reset winget
# 返回的命令输出
Resetting source: winget...Done

然而在安装软件时仍然会出现如下错误:

# 命令
PS C:\WINDOWS\system32> winget install --id Microsoft.PowerShell --source winget
# 返回的命令输出
Found PowerShell [Microsoft.PowerShell] Version 7.5.3.0
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Downloading https://github.com/PowerShell/PowerShell/releases/download/v7.5.3/PowerShell-7.5.3-win-x64.msi
An unexpected error occurred while executing the command:
InternetOpenUrl() failed.
0x80072efd : unknown error

由于众所周知的原因,我们还是没有稳定的互联网下载所要安装的软件,下面我们来介绍一个非常实用的功能—代理。

在 winget 中启用代理功能

启用 winget 的代理功能

# 命令
PS C:\WINDOWS\system32> winget settings --enable ProxyCommandLineOptions
# 返回的命令输出
Enabled admin setting 'ProxyCommandLineOptions'.

使用代理工具安装所需软件

使用代理前,必须已配置好代理工具,且代理工具处于工作状态。

# 命令
PS C:\WINDOWS\system32> winget install --id Microsoft.PowerShell --source winget --proxy http://127.0.0.1:7890
# 返回的命令输出
Found PowerShell [Microsoft.PowerShell] Version 7.5.3.0
This application is licensed to you by its owner.
Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Downloading https://github.com/PowerShell/PowerShell/releases/download/v7.5.3/PowerShell-7.5.3-win-x64.msi
  ██████████████████████████████   108 MB /  108 MB
Successfully verified installer hash
Starting package install...
Successfully installed

使用代理后我们的软件就可以快速地完成安装了。