Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

powershell

From the command line I can do.

.\test.ps1 1

How do I pass the parameter when doing this from C#? I've tried

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

And I have tried passing values in as IEnumerable<object> in the .Invoke() but $p does not get the value.

namespace ConsoleApplication1
{
    using System;
    using System.Linq;
    using System.Management.Automation;

    class Program
    {
        static void Main()
        {
            // Contents of ps1 file
            //  param($p)
            //  "Hello World ${p}"

            var script = @".\test.ps1";

            PowerShell
                .Create()
                .AddScript(script)
                .Invoke().ToList()
                .ForEach(Console.WriteLine);
        }
    }
}
like image 309
Doug Finke Avatar asked Jul 10 '12 01:07

Doug Finke


People also ask

How do you pass the parameter?

Parameter passing involves passing input parameters into a module (a function in C and a function and procedure in Pascal) and receiving output parameters back from the module. For example a quadratic equation module requires three parameters to be passed to it, these would be a, b and c.

Can we pass parameters by reference in C?

Passing by by reference refers to a method of passing the address of an argument in the calling function to a corresponding parameter in the called function. In C, the corresponding parameter in the called function must be declared as a pointer type.

Can I pass a value as a parameter?

You can pass values to the parameters that are declared in the AccessProfile widget by reference, by value, or by specifying the direct value. Define this option in the main AccessProfile. This topic provides an example of an AccessProfile widget and a main AccessProfile.

What is parameterized function in C?

Parameterized functions are functions that have a type parameter in their argument list (or at least the return type). There are two types of parameterized types in C++/CLI: templates, which are inherited from C++, and generics, which are the CLI parameterized type.


2 Answers

How's this?

static void Main()
{
    string script = @"C:\test.ps1 -arg 'hello world!'";
    StringBuilder sb = new StringBuilder();

    PowerShell psExec = PowerShell.Create();
    psExec.AddScript(script);
    psExec.AddCommand("out-string");

    Collection<PSObject> results;
    Collection<ErrorRecord> errors;
    results = psExec.Invoke();
    errors = psExec.Streams.Error.ReadAll();

    if (errors.Count > 0)
    {
        foreach (ErrorRecord error in errors)
        {
            sb.AppendLine(error.ToString());
        }
    }
    else
    {
        foreach (PSObject result in results)
        {
            sb.AppendLine(result.ToString());
        }
    }

    Console.WriteLine(sb.ToString());
}

Here's a similar version that passes an instance of a DateTime

static void Main()
{
    StringBuilder sb = new StringBuilder();

    PowerShell psExec = PowerShell.Create();
    psExec.AddCommand(@"C:\Users\d92495j\Desktop\test.ps1");
    psExec.AddArgument(DateTime.Now);

    Collection<PSObject> results;
    Collection<ErrorRecord> errors;
    results = psExec.Invoke();
    errors = psExec.Streams.Error.ReadAll();

    if (errors.Count > 0)
    {
        foreach (ErrorRecord error in errors)
        {
            sb.AppendLine(error.ToString());
        }
    }
    else
    {
        foreach (PSObject result in results)
        {
            sb.AppendLine(result.ToString());
        }
    }

    Console.WriteLine(sb.ToString());
}
like image 133
Elijah W. Gagne Avatar answered Oct 26 '22 04:10

Elijah W. Gagne


So, to make a long answer short: Use AddCommand instead of AddScript

like image 29
Willem M. Avatar answered Oct 26 '22 03:10

Willem M.