ファイルに保存されている PowerShell スクリプトがあります。Windows PowerShell では、スクリプトを次のように実行します。
.\MergeDocuments.ps1 "1.docx" "2.docx" "merge.docx"
C#からスクリプトを呼び出したい。現在、完全に機能する次のように Process.Start を使用しています。
Process.Start(POWERSHELL_PATH, string.Format("-File \"{0}\" {1} {2}", SCRIPT_PATH, string.Join(" ", filesToMerge), outputFilename));
以下のコードのようなクラスを使用して実行したいのです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();