This post was originally published here

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:BizTalkApplicationsMonitorMonitor_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"
}

The post Error while calling Logic App thru PowerShell: The underlying connection was closed: An unexpected error occurred on a send. appeared first on SANDRO PEREIRA BIZTALK BLOG.