使用 C# 中的参数调用 PowerShell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10260597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:04:42  来源:igfitidea点击:

Invoking PowerShell Script with Arguments from C#

c#powershell

提问by sh_kamalh

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"

我有一个 PowerShell 脚本存储在一个文件中。在 Windows PowerShell 中,我将脚本执行为
.\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));

我想从 C# 调用脚本。目前我正在使用 Process.Start 如下,它完美地工作:
Process.Start(POWERSHELL_PATH, string.Format("-File \"{0}\" {1} {2}", SCRIPT_PATH, string.Join(" ", filesToMerge), outputFilename));

I want to run it using Pipelineclass, 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)

我想使用Pipeline类运行它,类似于下面的代码,但我不知道如何传递参数(请记住,我没有命名参数,我只是使用 $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();

采纳答案by sh_kamalh

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");

为了将参数传递给 $args 传递 null 作为参数名称,例如 command.Parameters.Add(null, "some value");

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

该脚本被称为:
.\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();
    }
}