首页 > 解决方案 > “Invoke-WebRequest:底层连接已关闭:意外错误

问题描述

下面是我的自定义脚本扩展中的代码

$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -OutFile $env:C:\temp\azcopy.zip

当我运行自定义脚本扩展时,出现以下错误

"Invoke-WebRequest : The underlying connection was closed: An unexpected error

有关如何解决它的任何帮助?

标签: .netazurepowershell

解决方案


该错误非常具体,这使得这可能是您的主机或企业环境中的环境问题,因为您发布的代码应该/确实按原样工作。

# Tested on a few lab hosts
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -OutFile $env:C:\temp\azcopy.zip -Verbose
Get-ChildItem C:\temp

# Results
<#
VERBOSE: GET https://aka.ms/downloadazcopy-v10-windows with 0-byte payload
VERBOSE: received 9317519-byte response of content-type application/zip


    Directory: C:\temp


Mode                 LastWriteTime         Length Name                                                   
----                 -------------         ------ ----                                                   
-a----         9/11/2020   8:45 PM        9317519 azcopy.zip                                             
#>

然而,您实际上只需要在代码顶部使用它。

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

您可以通过运行代码来了解有关错误的更多信息,让错误发生然后使用

$Error[0] | Format-List -Force

您还可以使用Trace-Command cmdlet 了解更多信息。

Trace-Command -Name metadata,parameterbinding,cmdlet -Expression {
    Invoke-WebRequest -Uri https://aka.ms/downloadazcopy-v10-windows -OutFile $env:C:\temp\azcopy.zip
} -PSHost
# Results
<#
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Invoke-WebRequest]
DEBUG: ParameterBinding Information: 0 :     BIND arg [https://aka.ms/downloadazcopy-v10-windows] to parameter [Uri]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [System.Uri]
DEBUG: ParameterBinding Information: 0 :             Trying to convert argument value from System.String to System.Uri
DEBUG: ParameterBinding Information: 0 :             CONVERT arg type to param type using LanguagePrimitives.ConvertTo
DEBUG: ParameterBinding Information: 0 :             CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [https://aka.ms/downloadazcopy-v10-windows]
DEBUG: ParameterBinding Information: 0 :         Executing VALIDATION metadata: [System.Management.Automation.ValidateNotNullOrEmptyAttribute]
DEBUG: ParameterBinding Information: 0 :         BIND arg [https://aka.ms/downloadazcopy-v10-windows] to param [Uri] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 :     BIND arg [\temp\azcopy.zip] to parameter [OutFile]
DEBUG: ParameterBinding Information: 0 :         COERCE arg to [System.String]
DEBUG: ParameterBinding Information: 0 :             Parameter and arg types the same, no coercion is needed.
DEBUG: ParameterBinding Information: 0 :         BIND arg [\temp\azcopy.zip] to param [OutFile] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Invoke-WebRequest]
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Invoke-WebRequest]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing
#>

当尝试访问远程资源时,最好在采取行动之前测试活跃度/连接。通常这是一个 Test-Connection/Test-NetConection 的事情,但在你的情况下,其中一个 Invoke-* cmdlet 会更谨慎。

调用-WebRequest -Uri 'https://aka.ms/downloadazcopy-v10-windows' -UseBasicParsing

# Results
<#
StatusCode        : 200
StatusDescription : OK
Content           : {80, 75, 3, 4...}
RawContent        : HTTP/1.1 200 OK
                    Content-MD5: HjZjoPa87mg1bK4eVQqASQ==
                    x-ms-request-id: aa4341fb-e01e-00a6-0c94-810077000000
                    x-ms-version: 2009-09-19
                    x-ms-lease-status: unlocked
                    x-ms-blob-type: BlockBlob
                    Connect...
Headers           : {[Content-MD5, HjZjoPa87mg1bK4eVQqASQ==], [x-ms-request-id, aa4341fb-e01e-00a6-0c94-810077000000], [x-ms-version, 2009-09-19], 
                    [x-ms-lease-status, unlocked]...}
RawContentLength  : 9317519
#>

另请参阅这篇文章


推荐阅读