'Waiting for .exe output in Azure Function Apps
I am trying to receive the output from an .exe application in an Azure Function App. The Function app is triggered by a HTTP request.
The function app triggers and runs the executable. I then want to wait for the .exe to run and return the output as a Json string in the HTTP response if possible. Here is my code:
namespace FunctionApp
{
public static class NmapFunction
{
[FunctionName("NmapFunction")]
public static async Task<HttpResponseMessage> Run(
// wait for HTTP trigger on et or post method. Route is defualt.
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string output = string.Empty;
//Start the nmap .exe
var Process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "Nmap.exe",
Arguments = "-sS -Pn 127.0.0.1",
UseShellExecute = true,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
Process.Start();
while (!Process.StandardOutput.EndOfStream)
{
string line = Process.StandardOutput.ReadLine();
//add each line to the output string
output += line;
}
Process.WaitForExit();
string toolOutput = Newtonsoft.Json.JsonConvert.SerializeObject(new ToolRunResult { Result = output }).ToString();
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(ToolOutput, Encoding.UTF8, "application/json")
};
}
I am receiving a 500 internal server error when I run the HTTP request. This is because of an System.InvalidOperationException' in System.Diagnostics.Process.dll error in the while loop. I believe this is because this is a synchromous action and as i understand Azure function apps must be asynchronous.
So, my question is What is the best way to get the output from this .exe process? Is it even possible?
The ToolRunResult class:
internal class ToolRunResult
{
internal int StatusCode { get; set; }
internal string Result { get; set; }
}
Solution 1:[1]
What is the best way to get the output from this .exe process? Is it even possible?
Yes, It is possible in Azure Function.
Please follow the Workaround to run the nMap.exe in Azure Function
Here I am using a Timer trigger to run the .exe file and capturing the Output response of the .exe.
public void Run([TimerTrigger("0 */3 * * * *")]TimerInfo myTimer, ILogger log)
{
var p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
string eOut = null;
p.StartInfo.RedirectStandardError = true;
p.ErrorDataReceived += new DataReceivedEventHandler((sender, e) =>
{ eOut += e.Data; });
p.StartInfo.FileName = "C:\\Program Files (x86)\\Nmap\\nmap.exe";
p.StartInfo.Arguments = "-sS -Pn 127.0.0.1";
p.Start();
// To avoid deadlocks, use an asynchronous read operation on at least one of the streams.
p.BeginErrorReadLine();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
log.LogInformation($"The nMap Exe Result are:\n'{output}'");
log.LogInformation($"\nError stream: {eOut}");
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
Result

Solution 2:[2]
Error was in the StartInfo Variable. UseShellExecute must be set to false.
Unsure of the reasoning behind this, but it did the trick.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DelliganeshS-MT |
| Solution 2 | LiamWBA |

