“ Invoke-WebRequest:基础连接已关闭:意外错误

时间:2020-09-11 22:36:43

标签: .net azure powershell

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

$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

有任何解决方法的帮助吗?

1 个答案:

答案 0 :(得分:1)

该错误非常具体,这可能会导致主机或企业环境中的环境问题,因为您发布的代码应该/确实可以正常工作。

# 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
#>

另请参阅此article