Error while calling Logic App thru PowerShell: The underlying connection was closed: An unexpected error occurred on a send.

This week while I was implementing a BizTalk Server monitoring solution using:

  • PowerShell: for querying the environment;
  • Logic Apps: creating the flow logic for notifying the non-compliances
  • Function App: to convert the JSON object to HTML

while I was trying to invoke a Logic App with a Request trigger from PowerShell:

{
$jsonDoc = [pscustomobject]@{
    Monitor = "Disk Space Monitoring"
    Client = "Sandro Pereira"
    Environment = "DEV"
    Disks = $diskNode
}

Invoke-WebRequest -Uri 'https://{URi}.logic.azure.com:443/workflows/{guid}/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig={sig}' -Method POST -Body ($jsonDoc|ConvertTo-Json) -ContentType "application/json"
}

I got the following error:

Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At C:\BizTalkApplications\Monitor\Monitor_BizTalk_DiskSpaceStorage_with_Flow.ps1:77 char:1
+ Invoke-WebRequest -Uri ‘https://{URI}.logic.azure.com:44 …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Cause

I had already experienced something similar when communicating with Logic Apps from the BizTalk Server Logic App adapter. However, I had already forgotten about it.

But in fact, the essence of this error and the one I got with the BizTalk adapter is the same.

The Logic App Request trigger supports only Transport Layer Security (TLS) 1.2 for incoming calls. Outgoing calls continue to support TLS 1.0, 1.1, and 1.2.

Solution

The solution was, and is, very simple, we just need to enforce PowerShell to use TLS 1.2. This can be done using this PowerShell one-liner:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
{
$jsonDoc = [pscustomobject]@{
    Monitor = "Disk Space Monitoring"
    Client = "Sandro Pereira"
    Environment = "DEV"
    Disks = $diskNode
}

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri 'https://{URi}.logic.azure.com:443/workflows/{guid}/triggers/manual/paths/invoke?api-version=2016-10-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig={sig}' -Method POST -Body ($jsonDoc|ConvertTo-Json) -ContentType "application/json"
}
#1 Azure Monitoring Platform
Author: Sandro Pereira

Sandro Pereira lives in Portugal and works as a consultant at DevScope. In the past years, he has been working on implementing Integration scenarios both on-premises and cloud for various clients, each with different scenarios from a technical point of view, size, and criticality, using Microsoft Azure, Microsoft BizTalk Server and different technologies like AS2, EDI, RosettaNet, SAP, TIBCO etc. He is a regular blogger, international speaker, and technical reviewer of several BizTalk books all focused on Integration. He is also the author of the book “BizTalk Mapping Patterns & Best Practices”. He has been awarded MVP since 2011 for his contributions to the integration community.

Leave a Reply

Your email address will not be published. Required fields are marked *

turbo360

Back to Top