底层连接已关闭:接收时发生意外错误。从 Azure powershell Runbook 使用 Invoke-RestMethod 时

Sas*_*iev 7 powershell json httprequest azure

我正在尝试从 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
Run Code Online (Sandbox Code Playgroud)

我的脚本的第 43 行看起来像这样

Invoke-RestMethod -uri $uriHangouts -Method Post -body $body -ContentType 'application/json'
Run Code Online (Sandbox Code Playgroud)

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

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

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Run Code Online (Sandbox Code Playgroud)

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

请帮忙!先感谢您!

Hur*_*hen 15

您不需要使用Invoke-WebRequest相反Invoke-RestMethod,您只需要\xe2\x80\x8b[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12在执行之前运行,Invoke-RestMethod本博客所述。

\n

如果你想改用Invoke-WebRequestInvoke-RestMethod你可以尝试使用 add -UseBasicParsingafterInvoke-WebRequest命令。

\n