Korlátozott futási tér létrehozásaCreating a constrained runspace

A teljesítmény-és biztonsági okokból érdemes lehet korlátozni a gazdagép-alkalmazás számára elérhető Windows PowerShell-parancsokat.For performance or security reasons, you might want to restrict the Windows PowerShell commands available to your host application. Ehhez hozzon létre egy üres System.Management.Automation.Runspaces.Initialsessionstate aSystem.Management.Automation.Runspaces.Initialsessionstate meghívásával . Hozzon létre * metódust, és csak az elérhető parancsokat adja hozzá.To do this you create an empty System.Management.Automation.Runspaces.Initialsessionstate by calling the System.Management.Automation.Runspaces.Initialsessionstate.Create* method, and then add only the commands you want available.

Egy olyan RunSpace használatával, amely csak a megadott parancsokat tölti be, jelentősen jobb teljesítményt nyújt.Using a runspace that loads only the commands that you specify provides significantly improved performance.

A System. Management. Automation. futási terek. Sessionstatecmdletentry osztály metódusait használhatja a kezdeti munkamenet-állapothoz tartozó parancsmagok definiálásához.You use the methods of the System.Management.Automation.Runspaces.Sessionstatecmdletentry class to define cmdlets for the initial session state.

A parancsokat privát módon is elvégezheti.You can also make commands private. A gazda alkalmazás saját parancsokat is használhat, de az alkalmazás felhasználói nem.Private commands can be used by the host application, but not by users of the application.

Parancsok hozzáadása üres RunSpaceAdding commands to an empty runspace

Az alábbi példa bemutatja, hogyan hozhat létre üres InitialSessionState, és hogyan adhat hozzá parancsokat.The following example demonstrates how to create an empty InitialSessionState and add commands to it.

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();
    }
  }
}

Parancsok magánjellegűvé tételeMaking commands private

A parancsot saját maga is elvégezheti, ha a System. Management. Automation. Commandinfo. láthatóság tulajdonságot a System. Management. Automation. SessionStateEntryVisibility Privateértékre állítja.You can also make a command private, by setting it's System.Management.Automation.Commandinfo.Visibility property to System.Management.Automation.SessionStateEntryVisibility Private. A gazda alkalmazás és más parancsok meghívhatják a parancsot, de az alkalmazás felhasználója nem.The host application and other commands can call that command, but the user of the application cannot. A következő példában a Get-ChildItem parancs privát.In the following example, the Get-ChildItem command is private.

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

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

Lásd még:See Also

InitialSessionState létrehozásaCreating an InitialSessionState