Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking PowerShell Script with Arguments from C#

Tags:

c#

powershell

I have a PowerShell script stored in a file. In Windows PowerShell, I execute the script as
.\MergeDocuments.ps1 "1.docx" "2.docx" "merge.docx"

I want to call the script from C#. Currently I am using Process.Start as follows which works perfectly:
Process.Start(POWERSHELL_PATH, string.Format("-File \"{0}\" {1} {2}", SCRIPT_PATH, string.Join(" ", filesToMerge), outputFilename));

I want to run it using Pipeline class, something like the below code but I don't know how to pass the arguments (keep in mind that I don't have named arguments, I am just using $args)

// create Powershell runspace
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();

RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");

// create a pipeline and feed it the script text (AddScript method) or use the filePath (Add method)
Pipeline pipeline = runspace.CreatePipeline();
Command command = new Command(SCRIPT_PATH);
command.Parameters.Add("", ""); // I don't have named paremeters
pipeline.Commands.Add(command);

pipeline.Invoke();
runspace.Close();
like image 968
sh_kamalh Avatar asked Apr 21 '12 15:04

sh_kamalh


People also ask

How do you pass arguments to a PowerShell script?

A default value will not work with a mandatory parameter. You can omit the =$true for advanced parameters of type boolean [Parameter(Mandatory)] . @Andrew First of all you have to change the type of the parameter to [string] . If you then want to pass a string as parameter you can use either ' or " .

How do you pass an argument to invoke an expression?

The only parameter Invoke-Expression has is Command . There is no native way to pass parameters with Invoke-Expression . However, instead, you can include them in the string you pass to the Command parameter.

How do I run a ps1 script from the command line?

ps1. Then, to execute the PowerShell script from the command line, launch the Windows command line by typing "cmd" into the search box and clicking the result. Type the full path of the PowerShell script, such as "C:\Example\example_script. ps1" and press enter.


1 Answers

Just found it in one of the comments to another question

In order to pass arguments to the $args pass null as the parameter name, e.g. command.Parameters.Add(null, "some value");

The script is called as:
.\MergeDocuments.ps1 "1.docx" "2.docx" "merge.docx"

Here is the full code:

class OpenXmlPowerTools
{
    static string SCRIPT_PATH = @"..\MergeDocuments.ps1";

    public static void UsingPowerShell(string[] filesToMerge, string outputFilename)
    {
        // create Powershell runspace
        Runspace runspace = RunspaceFactory.CreateRunspace();
        runspace.Open();

        RunspaceInvoke runSpaceInvoker = new RunspaceInvoke(runspace);
        runSpaceInvoker.Invoke("Set-ExecutionPolicy Unrestricted");

        // create a pipeline and feed it the script text
        Pipeline pipeline = runspace.CreatePipeline();
        Command command = new Command(SCRIPT_PATH);
        foreach (var file in filesToMerge)
        {
            command.Parameters.Add(null, file);
        }
        command.Parameters.Add(null, outputFilename);
        pipeline.Commands.Add(command);

        pipeline.Invoke();
        runspace.Close();
    }
}
like image 146
sh_kamalh Avatar answered Sep 30 '22 04:09

sh_kamalh