更新时间:2022-11-07 21:33:13
只是在对另一个问题的评论中找到了它
Just found it in one of the comments to another question
为了将参数传递给$ args,请将null用作参数名称,例如 command.Parameters.Add(null, some value);
In order to pass arguments to the $args pass null as the parameter name, e.g. command.Parameters.Add(null, "some value");
该脚本称为: .\MergeDocuments.ps1 1.docx 2.docx merge.docx
这里是完整代码:
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();
}
}