2010年12月1日水曜日

C# Powershellスクリプトを実行

  • このエントリーをはてなブックマークに追加


Exchangeコマンドを含むPowershellスクリプトを実行するコードです。
 System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
//ComSpecのパスを取得する
psi.FileName = System.Environment.GetEnvironmentVariable("ComSpec");
//出力を読み取れるようにする
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
//ウィンドウを表示しないようにする
psi.CreateNoWindow = true;
//コマンドラインを指定("/c"は実行後閉じるために必要)
psi.Arguments = @" /c powershell.exe -PSConsoleFile ""C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1"" -command "".'" + psFile + @"'"" 2> """ + logFile + "\"";
//起動
System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
p.WaitForExit()
p.Close();

あるいは

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec");
process.StartInfo.RedirectStandardInput = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.Arguments = @" /c powershell.exe -PSConsoleFile ""C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1"" -command "".'" + psFile + @"'"" 2> """ + logFile + "\"";
process.Start();
process.BeginOutputReadLine();
process.WaitForExit();


Windows PowerShellクックブック

新品価格
¥4,200から
(2012/12/20 21:06時点)

この記事がお役にたちましたらシェアをお願いします:)

  • このエントリーをはてなブックマークに追加

0 件のコメント:

コメントを投稿