当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 创建受限运行空间

作者:精品下载站 日期:2024-12-14 02:46:24 浏览:12 分类:玩电脑

创建受限运行空间


出于性能或安全原因,您可能希望限制主机应用程序可用的 Windows PowerShell 命令。为此,您可以通过调用 System.Management.Automation.Runspaces.Initialsessionstate.Create* 方法创建一个空的 System.Management.Automation.Runspaces.Initialsessionstate,然后仅添加您希望可用的命令。

使用仅加载您指定的命令的运行空间可显着提高性能。

您可以使用 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();

参见

创建初始会话状态

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯