The underlying connection was closed: An unexpected error occurred on a receive. when using Invoke-RestMethod from Azure powershell runbook

12,592

Solution 1

You do not need to use Invoke-WebRequest instead Invoke-RestMethod, you just need to run ​[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 before executing Invoke-RestMethod as this blog mentioned.

And if you want use Invoke-WebRequest instead Invoke-RestMethod, you can have a try with add -UseBasicParsing after Invoke-WebRequest command.

Solution 2

After months of exchanging emails with Microsoft, it is officially confirmed bug. Below is their response.

Resolution: We have identified this issue as a platform bug and we are working on it.

Upgrading the Automation Account Platform version is going to happen This is a major change for Automation and it will take few months to roll this out, but at this moment we cannot provide an ETA for the same.

In the mean time as a workaround, they are suggesting to implement a retry mechanism in the runbook as the issue is happening intermittently. ​

Share:
12,592

Related videos on Youtube

Sasho Zahariev
Author by

Sasho Zahariev

Updated on June 04, 2022

Comments

  • Sasho Zahariev
    Sasho Zahariev almost 2 years

    I'm trying to send Microsoft Azure Alerts to Google Chat room (Hangouts) from within Azure Powershell Runbook. From time to time the message is sent without any issues, but roughly in half of the tries it returns the error below:

    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
    

    Line 43 of my script looks like this

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

    Where $uriHangouts is the webhook address and the $body is message according to the schema provided here: https://developers.google.com/hangouts/chat/reference/message-formats/basic

    As it is testing only, I'm sending every time exactly the same message. It is a basic one with some markup. If I forward it to Azure Logic App and resend it from there to Hangouts, it works every time. If I send it from my PC, it works every time as well. So far, I've tried without any success to use Invoke-WebRequest instead Invoke-RestMethod, and force TLS 1.2 (1.1) with the following line:

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

    I'm literally out of ideas where to go next.

    Please, help! Thank you in advance!

  • Koder101
    Koder101 almost 2 years
    Do you have any tracking page ( may be github issue ) where Microsoft is maintaining the status??