Trigger logic apps using Powershell or C#

2019-12-29 | apurvghai | Logic Apps | JSON, Powershell, C#


I was recently doing a project at work where I needed to trigger logic apps inside Build Pipeline. So I chose powershell as my weapon here. Powershell tasks are readily available inside the DevOps Pipeline. If you are interested in reading more about how to use powershell inside devops please read this post.

Logic apps Setup

In order for us to trigger logic apps, the app must be designed to accept HTTP requests. To learn more about it, you can follow this link to official documentation.

Let’s pretend that we have following JSON payload

 {
   "FirstName": "John",
   "LastName": "Doe",
   "Title": "Software Engineer",
   "Email": "john.doe@something.com"
 }

Now we need to generate the schema using logic apps. The screen below demonstrates that


http-logic-trigger

Once we have completed the the initial setup, let’s go to the next step and execute our powershell.

Powershell script

The script below is fairly simple as all it does is sends HTTP request to logic apps URL with JSON data.

 
 $data =@{
    "FirstName"= "John"
    "LastName"= "Doe"
    "Title"= "Software Engineer"
    "Email"= "john.doe@something.com"
 }

 $json = $data | ConvertTo-Json
 
 $LogicAppsUrl = "https://yourlogicappsurl"
 $LogicAppInfo = Invoke-WebRequest -Uri $LogicAppsUrl -Headers @{
    "Content-Type" = "application/json"
 } -Method Post -Body $json  -UseBasicParsing

 Write-Host "Trigger Complete"

C# Script


 public class LogicAppsModel {
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public string Title {get; set;}
    public string Email {get; set;}
 }

 public async Task SendRequestAsync() {
    var logicAppsModel = new LogicAppsModel();
    var logicAppsUrl = "yoururl";
    var client = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Post, logicAppsUrl);
    request.Content = new StringContent(JsonConvert.Serialize(logicAppsModel), Encoding.UTF8, "application/json");
    var response =  await client.SendAsync(request);
    Console.WriteLine("Trigger Complete");
 }

Note Make sure to include SAS signature in your logic apps url. https://autogeneratedazureserver.yourregion.logic.azure.com:443/workflows/yourworkflowid/triggers/request/paths/invoke?api-version=2016-10-01&sp=/triggers/request/run&sv=1.0&sig=yoursassignature

Hope you enjoyed reading it. Please leave your comments below.


Recent Posts