次の方法で共有


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

パフォーマンスやセキュリティ上の理由から、ホスト アプリケーションで使用できる Windows PowerShell コマンドを制限することが必要になる場合があります。 これを行うには、System.Management.Automation.Runspaces.InitialSessionState.Create* メソッドを呼び出して、System.Management.Automation.Runspaces.InitialSessionState 空のコマンドを作成し、使用可能なコマンドのみを追加します。

指定したコマンドのみを読み込む実行空間を使用すると、パフォーマンスが大幅に向上します。

System.Management.Automation.Runspaces.SessionStateCmdletEntry クラスのメソッドを使用して、初期セッション状態のコマンドレットを定義します。

コマンドをプライベートにすることもできます。 プライベート コマンドはホスト アプリケーションで使用できますが、アプリケーションのユーザーは使用できません。

空の実行空間へのコマンドの追加

次の例では、空の InitialSessionState を作成し、それにコマンドを追加する方法を示します。

namespace Microsoft.Samples.PowerShell.Runspaces
{
  using System;
  using System.Collections.ObjectModel;
  using System.Management.Automation;
  using System.Management.Automation.Runspaces;
  using Microsoft.PowerShell.Commands;
  using PowerShell = System.Management.Automation.PowerShell;

  /// <summary>
  /// This class contains the Main entry point for the application.
  /// </summary>
  internal class Runspace10b
  {
    /// <summary>
    /// This sample shows how to create an empty initial session state,
    /// how to add commands to the session state, and then how to create a
    /// runspace that has only those two commands. A PowerShell object
    /// is used to run the Get-Command cmdlet to show that only two commands
    /// are available.
    /// </summary>
    /// <param name="args">Parameter not used.</param>
    private static void Main(string[] args)
    {
      // Create an empty InitialSessionState and then add two commands.
      InitialSessionState iss = InitialSessionState.Create();

      // Add the Get-Process and Get-Command cmdlets to the session state.
      SessionStateCmdletEntry ssce1 = new SessionStateCmdletEntry(
                                                            "Get-Process",
                                                            typeof(GetProcessCommand),
                                                            null);
      iss.Commands.Add(ssce1);

      SessionStateCmdletEntry ssce2 = new SessionStateCmdletEntry(
                                                            "Get-Command",
                                                            typeof(GetCommandCommand),
                                                            null);
      iss.Commands.Add(ssce2);

      // Create a runspace.
      using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
      {
        myRunSpace.Open();
        using (PowerShell powershell = PowerShell.Create())
        {
          powershell.Runspace = myRunSpace;

          // Create a pipeline with the Get-Command command.
          powershell.AddCommand("Get-Command");

          Collection<PSObject> results = powershell.Invoke();

          Console.WriteLine("Verb                 Noun");
          Console.WriteLine("----------------------------");

          // Display each result object.
          foreach (PSObject result in results)
          {
            Console.WriteLine(
                             "{0,-20} {1}",
                             result.Members["verb"].Value,
                             result.Members["Noun"].Value);
          }
        }

        // Close the runspace and release any resources.
        myRunSpace.Close();
      }

      System.Console.WriteLine("Hit any key to exit...");
      System.Console.ReadKey();
    }
  }
}

コマンドをプライベートにする

System.Management.Automation.CommandInfo.Visibility プロパティ System.Management.Automation.SessionStateEntryVisibility Privateするように設定することで、コマンドをプライベートにすることもできます。 ホスト アプリケーションとその他のコマンドは、そのコマンドを呼び出すことができますが、アプリケーションのユーザーは呼び出すことができません。 次の例では、Get-ChildItem コマンドはプライベートです。

defaultSessionState = InitialSessionState.CreateDefault();
commandIndex = GetIndexOfEntry(defaultSessionState.Commands, "Get-ChildItem");
defaultSessionState.Commands[commandIndex].Visibility = SessionStateEntryVisibility.Private;

this.runspace = RunspaceFactory.CreateRunspace(defaultSessionState);
this.runspace.Open();

こちらもご覧ください

InitialSessionState の作成に関する