Azure Ubuntu服务器PowerShell中Az模块安装失败求助
在Azure Ubuntu服务器的PowerShell中安装Az模块失败:无法找到PSGallery仓库
我正尝试在Azure Linux服务器的Ubuntu系统上使用PowerShell安装Az模块,但PowerShell始终无法找到PSGallery仓库。我怀疑服务器的DNS解析存在问题,因为解析到的IP来自trafficmanager.net,且无法建立连接。恳请提供解决思路。
安装尝试
# 启动PowerShell PowerShell 7.3.4 PS /root> Install-Module -Name Az -Repository PSGallery -Force Get-PackageSource: 无法找到仓库 'PSGallery'。请使用 Get-PSRepository 查看所有可用仓库。 PS /root> [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 PS /root> Register-PSRepository -Default -Verbose VERBOSE: 正在对目标“PowerShellGet 提供程序中的模块仓库 'PSGallery' ()”执行操作“Register Module Repository.”。 PS /root> Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted Set-PSRepository: 未找到名为 'PSGallery' 的仓库。
DNS查询结果
scs3:~$ nslookup www.powershellgallery.com Server: 127.0.0.53 Address: 127.0.0.53#53 非权威回答: www.powershellgallery.com 规范名称 = powershellgallerytrafficmanager.trafficmanager.net. powershellgallerytrafficmanager.trafficmanager.net 规范名称 = psg-prod-eastus.cloudapp.net. Name: psg-prod-eastus.cloudapp.net Address: 172.174.17.44 # 查询redhat.com验证DNS功能 scs3:~# nslookup redhat.com Server: 127.0.0.53 Address: 127.0.0.53#53 非权威回答: Name: redhat.com Address: 34.235.198.240 Name: redhat.com Address: 52.200.142.250
故障排查操作
PS /root> Invoke-WebRequest 'https://www.powershellgallery.com/api/v2/' Invoke-WebRequest: 资源暂时不可用
# 使用curl测试连接 scs3:~# curl -v https://www.powershellgallery.com/api/v2/ * Trying 172.174.17.44:443... * TCP_NODELAY set * connect to 172.174.17.44 port 443 failed: Connection timed out * Failed to connect to www.powershellgallery.com port 443: Connection timed out * Closing connection 0 curl: (28) Failed to connect to www.powershellgallery.com port 443: Connection timed out # 重新启动PowerShell并检查支持的安全协议 [root@scsdevvem02 22Jun2023 12:42:32 ~]# pwsh PowerShell 7.3.4 PS /root> [enum]::GetNames([Net.SecurityProtocolType]) SystemDefault Ssl3 Tls Tls11 Tls12 Tls13
修改/etc/hosts后的尝试
在/etc/hosts中添加:
20.84.181.62 www.powershellgallery.com
执行安装命令:
PS /root> Install-Module -Name Az -Repository PSGallery -Force -verbose VERBOSE: 正在获取程序集的提供程序: /opt/microsoft/powershell/7/Modules/PackageManagement/coreclr/netstandard2.0/Microsoft.PackageManagement.ArchiverProviders.dll VERBOSE: 正在获取程序集的提供程序: /opt/microsoft/powershell/7/Modules/PackageManagement/coreclr/netstandard2.0/Microsoft.PackageManagement.CoreProviders.dll VERBOSE: 正在获取程序集的提供程序: /opt/microsoft/powershell/7/Modules/PackageManagement/coreclr/netstandard2.0/Microsoft.PackageManagement.MetaProvider.PowerShell.dll VERBOSE: 正在获取程序集的提供程序: /opt/microsoft/powershell/7/Modules/PackageManagement/coreclr/netstandard2.0/Microsoft.PackageManagement.NuGetProvider.dll Get-PackageSource: 无法找到仓库 'PSGallery'。请使用 Get-PSRepository 查看所有可用仓库。
解决思路
手动指定URL注册PSGallery仓库
默认的Register-PSRepository -Default可能因网络问题无法正确获取仓库配置,直接手动指定仓库源地址:Register-PSRepository -Name PSGallery -SourceLocation "https://www.powershellgallery.com/api/v2/" -InstallationPolicy Trusted执行后再用
Get-PSRepository确认仓库是否已注册。验证网络连通性
- 直接测试hosts中配置的IP是否可访问:
curl -v https://20.84.181.62/api/v2/ - 检查Azure服务器的NSG(网络安全组)出站规则,确保允许443端口的流量到该IP;同时确认服务器是否需要配置代理才能访问外部网络。
- 直接测试hosts中配置的IP是否可访问:
更新PowerShellGet和PackageManagement模块
旧版本的模块可能存在兼容性问题,若能临时连通网络,先更新这两个核心模块:Install-Module -Name PowerShellGet -Force -AllowClobber若无法连通,可从其他可访问PSGallery的机器下载模块压缩包,通过SCP传到服务器后离线安装。
确认PSGallery节点IP有效性
hosts中配置的IP可能已失效,可通过其他正常网络环境查询www.powershellgallery.com的有效IP,替换到hosts中再尝试。
内容的提问来源于stack exchange,提问作者Nash A
相关产品推荐
相关产品推荐

