1

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# ?

4
  • 1
    How about using environment variables? Commented Sep 26, 2017 at 13:17
  • How would you do that ? I looked it up but i couldn't find much info about it ... I tried my var's initialization to $global:htmlOutput, but it didn't change anything. I tried fetching the var like i did before, and i also tried adding global: before the var name in the call to GetVariable ... still get a null value :-/ Commented Sep 26, 2017 at 13:28
  • Reading/writing environment variables is straightforward in both powershell and C#. See ss64.com/ps/syntax-env.html. Commented Sep 26, 2017 at 13:33
  • Did you check powershell.Streams.Error content? Commented Sep 26, 2017 at 14:48

2 Answers 2

3

Thank you all for your help, you put me on the right tracks. Here is the final working code

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.AddScript(command);
powerShell.Invoke();
// GetVariable returns an "object", that is in fact an array of PSObjec
var lines = ((object[]) powerShell.Runspace.SessionStateProxy.GetVariable("htmlOutput")).Cast<PSObject>();
// Agregate all returned PSObjec (each one of them being a line from the powershell output) and aggregate them into a string
output = t.Aggregate(output, (current, item) => current + item.BaseObject.ToString()); 

Took a lot of brain-twisting but it does work.

Sign up to request clarification or add additional context in comments.

Comments

1

I've never done this before but seems like you are not the only one who has this question, check out the answers over at :

How to get value from powershell script into C# variable?

passing powershell variables to C# code within PS script

3 Comments

When using this method, i get a ps.Invoke() returning an empty list. The other one just won't work (i'm trying to debug it)
So i tried both, i get a list containing 0 result in both cases :-/
So i have no experience in C# and i can't really test with you but in that code the execution policy is set. That requires Administrative rights. Maybe run your code as an Administrator? Its a long shot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.