How to pass a parameter from C# to a PowerShell script file? -


I can do the command line

 . Test.ps1 1   

How do I pass the parameter from doing this? I have tried

  .AddArgument (1) .AddParameter ("p", 1)   

And I have tried to pass the values ​​like That IEnumerable & lt; Object & gt; In. () But do not get $ p value.

  Namespace ConsoleApplication1 {Using the system; Using System.Linq; Using System.Management.Automation; Class program {static zero main () {// theme of the ps1 file / paragraph ($ p) // "hello world $ {p}" var script = @ ". \ Test.ps1"; PowerShell Create () .addScript (script). Invoke () ToList (). First (console. WrightLine); }}    

How is it?

  static zero main () {string script = @ "c: \ test.ps1 -arg 'hello world!' '; Stringbuilder Sb = new string builder (); PowerShell psExec = PowerShell.Create (); psExec.AddScript (script); psExec.AddCommand ("out-string"); collection of & lt; PSObject & gt; results; & Lt; ErrorRecord & gt; Errors; Results = psExec.Invoke (); Errors = psExec.Streams.Error.ReadAll (); if (errors.Count & gt; 0) {foreach (error errors in error errors) {sb .AppendLine (error.ToString ());}} and {foreach (result in PSObject result) {sb.AppendLine (result.tostring);}} console.wrightline (sb.ToString ());}   

There is a similar version Date time

  static void main () {StringBuilder sb = passes an example of new StringBuilder (), PowerShell psExec = PowerShell.Create (); psExec.AddCommand (@ "C: \ Results: = psExec.Invoke () results in the & lt; ErrorRecord & gt; Users \ d92495j \ desktop \ test.ps1 "); psExec.AddArgument (DateTime.Now); 

PSObject & gt; ; Errors = psExec.Streams.Error.ReadAll (); If (errors.Count & gt; 0) {foreach (error errors in error errors) {sb.AppendLine (error.ToString ()); }} And {foreach (result in PSObject result) {sb.AppendLine (result.tostring ()); }} Console. WrightLine (sb.ToString ()); }

Comments