【问题标题】:The underlying connection was closed: An unexpected error occurred on a receive. when using Invoke-RestMethod from Azure powershell runbook底层连接已关闭:接收时发生意外错误。从 Azure Powershell Runbook 使用 Invoke-RestMethod 时
【发布时间】:2021-05-09 01:40:57
【问题描述】:

我正在尝试从 Azure Powershell Runbook 将 Microsoft Azure 警报发送到 Google 聊天室(环聊)。有时会发送消息没有任何问题,但大约有一半的尝试返回以下错误:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive. 
At line:43 char:1 
+ Invoke-RestMethod -uri $uriHangouts -Method Post -body $body -Content ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException 
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

我的脚本的第 43 行如下所示

Invoke-RestMethod -uri $uriHangouts -Method Post -body $body -ContentType 'application/json'

根据此处提供的架构,其中 $uriHangouts 是 webhook 地址,$body 是消息: https://developers.google.com/hangouts/chat/reference/message-formats/basic

由于它只是测试,我每次都发送完全相同的消息。这是一个带有一些标记的基本版本。 如果我将它转发到 Azure Logic App 并从那里重新发送到环聊,它每次都能正常工作。 如果我从我的 PC 发送它,它每次都可以正常工作。 到目前为止,我尝试使用 Invoke-WebRequest 代替 Invoke-RestMethod,但没有成功,并使用以下行强制 TLS 1.2 (1.1):

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

我真的不知道下一步该去哪里。

请帮忙! 提前谢谢!

【问题讨论】:

    标签: json azure powershell httprequest


    【解决方案1】:

    您不需要使用Invoke-WebRequest 代替Invoke-RestMethod,您只需要在执行Invoke-RestMethod 之前运行​[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12,正如this blog 提到的那样。

    如果您想使用Invoke-WebRequest 而不是Invoke-RestMethod,您可以尝试在Invoke-WebRequest 命令之后添加-UseBasicParsing

    【讨论】:

      【解决方案2】:

      在与 Microsoft 交换了数月的电子邮件后,它被正式确认为 bug。以下是他们的回应。

      分辨率: 我们已将此问题确定为平台错误,并且正在努力解决。

      即将升级自动化账户平台版本 这是自动化的重大变化,需要几个月的时间才能推出,但目前我们无法提供相同的 ETA。

      同时,作为一种解决方法,他们建议在运行手册中实施重试机制,因为问题是间歇性发生的。 ​

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-15
        • 2011-01-25
        • 2014-03-10
        相关资源
        最近更新 更多