Sra*_*uru 2 .net powershell azure
以下是我的自定义脚本扩展中的代码
$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
Run Code Online (Sandbox Code Playgroud)
当我运行自定义脚本扩展时,出现以下错误
"Invoke-WebRequest : The underlying connection was closed: An unexpected error
Run Code Online (Sandbox Code Playgroud)
关于如何解决它有任何帮助吗?
该错误非常具体,这使得这可能是您的主机或企业环境中的环境问题,因为您发布的代码应该/确实按原样工作。
# 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
#>
Run Code Online (Sandbox Code Playgroud)
然而,您实际上只需要在代码顶部使用它。
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Run Code Online (Sandbox Code Playgroud)
您可以通过运行代码来获取有关错误的更多信息,让错误发生,然后使用
$Error[0] | Format-List -Force
Run Code Online (Sandbox Code Playgroud)
您还可以使用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
#>
Run Code Online (Sandbox Code Playgroud)
当尝试访问远程资源时,最好在采取行动之前测试活动/连接。通常这是 Test-Connection/Test-NetConection 的事情,但在您的情况下, Invoke-* cmdlet 之一会更谨慎。
Invoke-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
#>
Run Code Online (Sandbox Code Playgroud)
另请参阅这篇文章。
| 归档时间: |
|
| 查看次数: |
5449 次 |
| 最近记录: |