何となく暇だったので以前から色々と書いていたC#からPowerShellを実行するメソッドについてをまとめたクラス「ExecPowerShell」を作成しました。
ソースについて
文字列型の配列にしか対応していなかったので、今回は指定できるようにしました。
// 引数なし public Collection<PSObject> ExecPowerShell(string psScriptName) // 引数あり public Collection<PSObject> ExecPowerShell<T>(string psScriptName, PSDataCollection<T> psdc)
サンプルコード
基本的な使い方は以前の記事を参考にしてもらえれば…
namespace PSExecTest
{
using ExecPowerShell;
using System.Management.Automation;
class Program
{
static void Main(string[] args)
{
ExecPS execPs = new ExecPS();
// 引数なしで実行
execPs.ExecPowerShell("./test.ps1");
// 文字列型の配列を渡して実行
string[] test = new string[] { "test", "test"};
PSDataCollection<string[]> psdc = new PSDataCollection<string[]>();
psdc.Add(test);
psdc.Complete();
execPs.ExecPowerShell<string[]>("./test.ps1", psdc);
// 数値型の配列を渡して実行
PSDataCollection<int[]> psdc2 = new PSDataCollection<int[]>();
psdc2.Add(new int[] {1,2});
psdc2.Complete();
execPs.ExecPowerShell<int[]>("./test.ps1", psdc2);
}
}
}
(´-`).。oO(疲れた…)
