I run a command in Powershell from C#, and I need to read the content of a var. I tried all the solutions I found online but none worked so far.
Here is my current code (with some "attempts" still included)
string output;
var nodePath = HttpContext.Server.MapPath("~/.bin/node.exe");
var mjmlFromStringCmd = HttpContext.Server.MapPath("~/.bin/mjmlFromString");
var mjmlTemplate = System.IO.File.ReadAllText(HttpContext.Server.MapPath("~/.bin/test.mjml"));
var command = $"$htmlOutput = {nodePath} {mjmlFromStringCmd} -c '{mjmlTemplate}'";
var powershell = PowerShell.Create();
powershell.Commands.AddScript(command);
var t = powershell.Invoke(); // I tired using powershell.Invoke()[0] because a post on SO said it might work, but the array returned by Invoke contains 0 elements
var bf = powershell.Runspace.SessionStateProxy.PSVariable.Get("htmlOutput"); // I tried to get the var using two different methods, both give an empty value
var htmlOutput = powershell.Runspace.SessionStateProxy.GetVariable("htmlOutput");
output = htmlOutput as string;
and here is the executed powershell command
$htmlOutput = C:\\Perso\\Websites\\FoyerRural\\.bin\\node.exe C:\\Perso\\Websites\\FoyerRural\\.bin\\mjmlFromString -c '****lots of content - MJML template (xml-style markup)****'
If I run the command directly in a powershell prompt, the $htmlOutput
var gets its value and I can print it by calling
$htmlOutput
Is there some trick with the C# powershell
class that I missed ? How can I get the value of the powershell htmlOutput
variable in C# ?