PowerShell安装NuGet,说无法访问互联网,但我实际上可以



我按照在代理后面使用 PowerShell 中提到的步骤来配置我的代理服务器。

netsh winhttp set proxy "[IP]:[Port]"
$Wcl = New-Object System.Net.WebClient
$Creds = Get-Credential
$Wcl.Proxy.Credentials = $Creds

弹出一个对话框,用于在第三行收集我的凭据。

然后我尝试安装 NuGet:

PS C:UsersAdministrator> Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/
?LinkID=627338&clcid=0x409' to ''.
WARNING: Unable to download the list of available providers. Check your internet
connection.
Install-PackageProvider : No match was found for the specified search criteria
for the provider 'NuGet'. The package provider requires 'PackageManagement' and
'Provider' tags. Please check if the specified package has the tags.
At line:1 char:1
+ Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Microsoft.Power...PackageProvider:InstallPackageProvider) [Install-PackageProvider], Exception
+ FullyQualifiedErrorId : NoMatchFoundForProvider,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackageProvider

错误消息似乎表明我的PowerShell无法连接到互联网,但是当我尝试此命令时:

PS C:UsersAdministrator> Invoke-WebRequest "https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409"
StatusCode        : 200
StatusDescription : OK
Content           : <?xml version="1.0" encoding="utf-8"?>
<SoftwareIdentity xmlns="http://standards.iso.org/iso/19770/-2/2015/schema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:discovery="http://p...
RawContent        : HTTP/1.1 200 OK
Content-MD5: 01uMBNqAHedJsS1dqKC5oA==
Vary: Accept-Encoding
X-Cache: HIT
x-ms-blob-type: BlockBlob
x-ms-lease-status: unlocked
x-ms-request-id: 1b7af5a7-901e-0003-5d94-f5cc950000...
Forms             : {}
Headers           : {[Content-MD5, 01uMBNqAHedJsS1dqKC5oA==], [Vary, Accept-Encoding], [X-Cache, HIT],
[x-ms-blob-type, BlockBlob]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        : System.__ComObject
RawContentLength  : 1847

看来它毕竟可以连接到互联网。

我做错了什么?如何安装 NuGet?

编辑: 我尝试了奥卡索·普罗塔尔的建议:

PS C:UsersAdministrator> Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -proxy [ProxyServer:Port] -proxycredential $Creds
WARNING: Unable to download from URI 'https://go.microsoft.com/fwlink/?LinkID=627338&clcid=0x409' to ''.
WARNING: Unable to download the list of available providers. Check your internet connection.
Install-PackageProvider : No match was found for the specified search criteria for the provider 'NuGet'. The package
provider requires 'PackageManagement' and 'Provider' tags. Please check if the specified package has the tags.
At line:1 char:1
+ Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (Microsoft.Power...PackageProvider:InstallPackageProvider) [Install-Pac
kageProvider], Exception
+ FullyQualifiedErrorId : NoMatchFoundForProvider,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackagePro
vider
PS C:UsersAdministrator> $Creds
UserName                                   Password
--------                                   --------
[My UserName] System.Security.SecureString

似乎没有效果。

可能与TLS安全相关(参考: https://rnelson0.com/2018/05/17/powershell-in-a-post-tls1-1-world/)

首先尝试以下命令:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

然后尝试进行更新。 注意:该命令仅影响当前会话,不会持续存在。

您还可以检查计算机上设置的客户端TLS版本。看起来客户端需要 TLS 1.0。(参考: https://powershell.org/forums/topic/wmf-5-1-upgrade-broken-repositories/)

迈克尔

根据 https://community.spiceworks.com/topic/2265662-powershell-get-download-problem

我今天遇到了同样的问题...

我的问题是PowerShell图库不会在W2016盒子上下载任何内容:

VERBOSE: InstallPackage' - name='AzureRM.BootStrapper',
version='0.5.0',destination='C:UsersAdministratorAppDataLocalTemp21254134668'
VERBOSE: DownloadPackage' - name='AzureRM.BootStrapper',
version='0.5.0',destination='C:UsersAdministratorAppDataLocalTemp21254134668AzureRM.BootStrapperAzureRM.BootSt
rapper.nupkg', uri='https://www.powershellgallery.com/api/v2/package/AzureRM.BootStrapper/0.5.0'
VERBOSE: Downloading 'https://www.powershellgallery.com/api/v2/package/AzureRM.BootStrapper/0.5.0'.
VERBOSE: An error occurred while sending the request.
VERBOSE: Retry downloading 'https://www.powershellgallery.com/api/v2/package/AzureRM.BootStrapper/0.5.0' for '2' more
times
VERBOSE: An error occurred while sending the request.
VERBOSE: Retry downloading 'https://www.powershellgallery.com/api/v2/package/AzureRM.BootStrapper/0.5.0' for '1' more
times
VERBOSE: An error occurred while sending the request.
VERBOSE: Retry downloading 'https://www.powershellgallery.com/api/v2/package/AzureRM.BootStrapper/0.5.0' for '0' more
times
VERBOSE: Downloading package 'AzureRM.BootStrapper' failed, please make sure
'https://www.powershellgallery.com/api/v2/package/AzureRM.BootStrapper/0.5.0' is accessable.
WARNING: Source Location 'https://www.powershellgallery.com/api/v2/package/AzureRM.BootStrapper/0.5.0' is not valid.
PackageManagementInstall-Package : Package 'AzureRM.BootStrapper' failed to download.
At C:Program FilesWindowsPowerShellModulesPowerShellGet1.0.0.1PSModule.psm1:1772 char:21
+ ...          $null = PackageManagementInstall-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : ResourceUnavailable: (C:UsersAdmini...tStrapper.nupkg:String) [Install-Package], Excep
tion
+ FullyQualifiedErrorId : PackageFailedInstallOrDownload,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPac
kage

帮助我发现它是TLS v1.2的是Fiddler中的此错误:

System.Security.Authentication.AuthenticationException A call to SSPI failed, see inner exception

从那里我得到了这个 -> 对 SSPI 的调用失败,请参阅内部异常 - 无法联系本地安全机构

然后使用以下命令在本地进行测试:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

效果很好!

希望这将在未来对某人有所帮助:-)

您可以通过编辑注册表为整个 .NET 框架设置它:

# Set strong cryptography on 64 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:SOFTWAREWow6432NodeMicrosoft.NetFrameworkv4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

# Set strong cryptography on 32 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:SOFTWAREMicrosoft.NetFrameworkv4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord 

这发生在几天前的Windows Server 2016数据中心盒子上,Win 2019仍然运行良好。

命令得益于: https://johnlouros.com/blog/enabling-strong-cryptography-for-all-dot-net-applications

快乐的 Azure 堆叠!!

试试这个:

[System.Net.WebRequest]::DefaultWebProxy.Credentials = System.Net.CredentialCache]::DefaultCredentials

如果上述方法不起作用,请尝试以下操作:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

在采取了许多步骤来克服类似的问题之后,我只想以更清晰的方式记录该过程。以下是克服Windows Server中NuGet和Docker安装问题的步骤(在2016年测试):

  1. 确保 Powershell 版本为 5.1 或更高版本: 获取主机 |选择对象版本

  2. 更新服务器支持的 SSL/TLS 版本:

x64:设置项属性 - 路径 'HKLM:\软件\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -name 'SchUseStrongCrypto' -value '1' -type DWord

x32:设置项属性 - 路径 'HKLM:\软件\Microsoft\.NetFramework\v4.0.30319' -name 'SchUseStrongCrypto' -value '1' -type DWord

  1. 重新启动Powershell并检查是否达到了预期的效果: [Net.ServicePointManager]::SecurityProtocol

(你现在应该看到这样的排序:) Tls、Tls11、Tls12 等

  1. >安装 NuGet: 安装包提供程序 -名称 NuGet -最低版本 2.8.5.201 -强制

  2. 安装 Docker: 安装模块 -名称 DockerMsftProvider -force 安装包 -名称 docker -提供程序名称 DockerMsftProvider -force

相关内容

最新更新