Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# PowerShell - AddScript as multiple statements

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?

like image 294
Martin K Avatar asked Oct 19 '25 02:10

Martin K


1 Answers

Simply use string concatenation to append the $? statement to the statements received, preceded by a ; (statement separator):

powerShell.AddScript(@"multiple powershell statements 
            on multiple rows, some of them writing to output" + ";$?");

That way, all statements are submitted as a single script [block] (piece of PowerShell code).

Successive .AddScript() calls aren't cumulative - the last one "wins".

Alternatively, append .AddStatement() to each .AddScript() call, analogously to how you're already doing for the .AddCommand() call that changes the execution policy.

However, it's more efficient to pass only a single piece of PowerShell code, using a single .AddScript() call, as shown above.


Finally, the obligatory security warning:

I receive the PowerShell script as a user input

Unless you check and potentially sanitize the input first, only execute it if you trust the source.

like image 126
mklement0 Avatar answered Oct 21 '25 17:10

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!