コマンドを追加し、呼び出す
実行空間を作成した後、Windows PowerShell のコマンドとスクリプトをパイプラインに追加し、パイプラインを同期的または非同期的に呼び出すことができます。
パイプラインの作成
System.Management.Automation.PowerShell クラスには、コマンド、パラメーター、スクリプトをパイプラインに追加するためのメソッドがいくつか用意されています。 パイプラインを同期的に呼び出すには、System.Management.Automation.PowerShell.Invoke* メソッドのオーバーロードを呼び出すか、System.Management.Automation.PowerShell.BeginInvoke* のオーバーロードを呼び出してから、System.Management.Automation.PowerShell.EndInvoke* メソッドを呼び出すことによって非同期的に呼び出すことができます。
AddCommand
System.Management.Automation.PowerShell オブジェクトを作成します。
PowerShell ps = PowerShell.Create();
実行するコマンドを追加します。
ps.AddCommand("Get-Process");
コマンドを呼び出します。
ps.Invoke();
System.Management.Automation.PowerShell.Invoke* メソッドを呼び出す前に、System.Management.Automation.PowerShell.AddCommand* メソッドを複数回呼び出すと、最初のコマンドの結果が 2 番目のコマンドにパイプ処理されます。 前のコマンドの結果をコマンドにパイプしない場合は、代わりに System.Management.Automation.PowerShell.AddStatement* を呼び出して追加します。
AddParameter
前の例では、パラメーターを指定せずに 1 つのコマンドを実行します。
System.Management.Automation.PSCommand.AddParameter* メソッドを使用して、コマンドにパラメーターを追加できます。たとえば、次のコードは、コンピューターで実行 powershell
という名前のすべてのプロセスの一覧を取得します。
PowerShell.Create().AddCommand("Get-Process")
.AddParameter("Name", "powershell")
.Invoke();
System.Management.Automation.PSCommand.AddParameter* を繰り返し呼び出すことで、パラメーターを追加できます。
PowerShell.Create().AddCommand("Get-Command")
.AddParameter("Name", "Get-VM")
.AddParameter("Module", "Hyper-V")
.Invoke();
System.Management.Automation.PowerShell.AddParameters* メソッドを呼び出して、パラメーター名と値のディクショナリを追加することもできます。
IDictionary parameters = new Dictionary<String, String>();
parameters.Add("Name", "Get-VM");
parameters.Add("Module", "Hyper-V");
PowerShell.Create().AddCommand("Get-Command")
.AddParameters(parameters)
.Invoke()
AddStatement
System.Management.Automation.PowerShell.AddStatement* メソッドを使用してバッチ処理をシミュレートできます。パイプラインの末尾にステートメントを追加します。次のコードは、powershell
という名前の実行中のプロセスの一覧を取得し、実行中のサービスの一覧を取得します。
PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-Process").AddParameter("Name", "powershell");
ps.AddStatement().AddCommand("Get-Service");
ps.Invoke();
AddScript
既存のスクリプトを実行するには、System.Management.Automation.PowerShell.AddScript* メソッドを呼び出します。 次の例では、パイプラインにスクリプトを追加して実行します。 この例では、D:\PSScripts
という名前のフォルダーに MyScript.ps1
という名前のスクリプトが既に存在することを前提としています。
PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"D:\PSScripts\MyScript.ps1")).Invoke();
useLocalScope
という名前のブール値パラメーターを受け取る System.Management.Automation.PowerShell.AddScript* メソッドのバージョンもあります。 このパラメーターが true
に設定されている場合、スクリプトはローカル スコープで実行されます。 次のコードは、ローカル スコープでスクリプトを実行します。
PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"D:\PSScripts\MyScript.ps1"), true).Invoke();
パイプラインを同期的に呼び出す
パイプラインに要素を追加したら、それを呼び出します。 パイプラインを同期的に呼び出すには、System.Management.Automation.PowerShell.Invoke* メソッドのオーバーロードを呼び出します。 次の例は、パイプラインを同期的に呼び出す方法を示しています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
namespace HostPS1e
{
class HostPS1e
{
static void Main(string[] args)
{
// Using the PowerShell.Create and AddCommand
// methods, create a command pipeline.
PowerShell ps = PowerShell.Create().AddCommand ("Sort-Object");
// Using the PowerShell.Invoke method, run the command
// pipeline using the supplied input.
foreach (PSObject result in ps.Invoke(new int[] { 3, 1, 6, 2, 5, 4 }))
{
Console.WriteLine("{0}", result);
} // End foreach.
} // End Main.
} // End HostPS1e.
}
パイプラインの非同期呼び出し
パイプラインを非同期的に呼び出すには、System.Management.Automation.PowerShell.BeginInvoke* のオーバーロードを呼び出して、IAsyncResult オブジェクトを作成し、System.Management.Automation.PowerShell.EndInvoke* メソッドを呼び出します。
次の例は、パイプラインを非同期的に呼び出す方法を示しています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
namespace HostPS3
{
class HostPS3
{
static void Main(string[] args)
{
// Use the PowerShell.Create and PowerShell.AddCommand
// methods to create a command pipeline that includes
// Get-Process cmdlet. Do not include spaces immediately
// before or after the cmdlet name as that will cause
// the command to fail.
PowerShell ps = PowerShell.Create().AddCommand("Get-Process");
// Create an IAsyncResult object and call the
// BeginInvoke method to start running the
// command pipeline asynchronously.
IAsyncResult asyncpl = ps.BeginInvoke();
// Using the PowerShell.Invoke method, run the command
// pipeline using the default runspace.
foreach (PSObject result in ps.EndInvoke(asyncpl))
{
Console.WriteLine("{0,-20}{1}",
result.Members["ProcessName"].Value,
result.Members["Id"].Value);
} // End foreach.
System.Console.WriteLine("Hit any key to exit.");
System.Console.ReadKey();
} // End Main.
} // End HostPS3.
}
こちらもご覧ください
InitialSessionState の作成に関する
制約付き実行空間 の作成
PowerShell