コマンドを追加し、呼び出す

実行空間を作成した後、PowerShell コマンドWindowsスクリプトをパイプラインに追加し、パイプラインを同期的または非同期的に呼び出します。

パイプラインを作成する

System.Management.Automation.Powershellクラスには、コマンド、パラメーター、スクリプトをパイプラインに追加するためのメソッドがいくつか含まれています。 パイプラインを同期的に呼び出すには 、System.Management.Automation.Powershell.Invoke* メソッドのオーバーロードを呼び出します。または 、System.Management.Automation.Powershell.Begininvoke* のオーバーロードを呼び出してから System.Management.Automation.Powershell.Endinvoke* メソッドを呼び出して非同期的に呼び出します。

AddCommand

  1. System.Management.Automation.Powershell オブジェクトを作成します。

    PowerShell ps = PowerShell.Create();
    
  2. 実行するコマンドを追加します。

    ps.AddCommand("Get-Process");
    
  3. コマンドを呼び出します。

    ps.Invoke();
    

System.Management.Automation.Powershell.Invoke* メソッドを呼び出す前に System.Management.Automation.Powershell.Addcommand*メソッドを 2 回目に呼び出した場合、最初のコマンドの結果は 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*メソッドを呼び出して、既存のスクリプトを実行できます。 次の例では、パイプラインにスクリプトを追加して実行します。 この例では、 という名前のフォルダーに という名前の MyScript.ps1 スクリプトが既に存在すると想定しています D:\PSScripts

PowerShell ps = PowerShell.Create();
ps.AddScript(File.ReadAllText(@"D:\PSScripts\MyScript.ps1")).Invoke();

という名前のブール型パラメーターを受け取る System.Management.Automation.Powershell.Addscript* メソッドのバージョンも用意されています useLocalScope 。 このパラメーターが に設定されている 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 を作成する

制約付き実行空間を作成する