ค้นหาเว็บไซต์

การสร้างพื้นที่วิ่งที่มีข้อจำกัด


เพื่อเหตุผลด้านประสิทธิภาพหรือความปลอดภัย คุณอาจต้องการจำกัดคำสั่ง Windows PowerShell ที่ใช้ได้กับแอปพลิเคชันโฮสต์ของคุณ เมื่อต้องการทำเช่นนี้ คุณต้องสร้าง System.Management.Automation.Runspaces.Initialssessionstate ที่ว่างเปล่าโดยการเรียกเมธอด System.Management.Automation.Runspaces.Initialsessionstate.Create* จากนั้นเพิ่มเฉพาะคำสั่งที่คุณต้องการให้ใช้งานได้

การใช้รันสเปซที่โหลดเฉพาะคำสั่งที่คุณระบุจะทำให้ประสิทธิภาพดีขึ้นอย่างมาก

คุณใช้วิธีการของคลาสSystem.Management.Automation.Runspaces.Sessionstatecmdletentryเพื่อกำหนด cmdlet ของสำหรับสถานะเซสชันเริ่มต้น

คุณยังสามารถทำให้คำสั่งเป็นแบบส่วนตัวได้ คำสั่งส่วนตัวสามารถใช้ได้โดยแอปพลิเคชันโฮสต์ แต่ไม่ใช่โดยผู้ใช้แอปพลิเคชัน

การเพิ่มคำสั่งลงในรันสเปซว่าง

ตัวอย่างต่อไปนี้สาธิตวิธีการสร้าง 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

บทความที่เกี่ยวข้อง