I am struggling to find a way to execute a multi-line and multi-statement PowerShell script in C# in such a way, that all the results will be available from the Invoke() method. It appears that the commands added by PowerShell.AddScript are always pipelined together. But I would like them to be read, parsed and executed as multiple statements with all of the results of the individual statements being written to the output.
Is that possible?
What I do now:
using PowerShell powerShell = PowerShell.Create();
powerShell.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted").AddParameter("Scope", "CurrentUser").AddStatement();
powerShell.AddScript(@"multiple powershell statements
on multiple rows, some of them writing to output"); //No intermediate results will be logged or printed as the script is added as a single pipeline
powerShell.AddScript("$?"); //This is to echo the execution status of the last command into results
var results = powerShell.Invoke();
The results collection contains only one result, and that is the echo of the last $? statement which I then query to find out pass/fail information. I also query Error stream but that is not relevant to the question.
The problem is: I receive the PowerShell script as a user input and therefore cannot modify it and add as statements. I was thinking about adding the script line by line, but it will break possible multiline pipelines, codeblocks etc. So to reach the result I want I would need to parse the script myself which sounds quite complex. Is there any elegant solution to receive all the results from Invoke() and most importantly, add a complex script from user input without it being crushed together as a single pipeline, feeding results of one command to another?