基於效能或安全性考慮,您可能會想要限制主應用程式可用的 Windows PowerShell 命令。 若要這樣做,您可以呼叫 System.Management.Automation.Runspaces.InitialSessionState 建立空的 System.Management.Automation.Runspaces.InitialSessionState.Create* 方法,然後只新增您想要的命令。
使用僅載入您指定之命令的 Runspace 可提供大幅改善的效能。
您可以使用 System.Management.Automation.Runspaces.SessionStateCmdletEntry 類別的方法,定義初始會話狀態的 Cmdlet。
您也可以將命令設為私用。 私人命令可以由主應用程式使用,但不能由應用程式的使用者使用。
將命令新增至空的 Runspace
下列範例示範如何建立空的 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.SessionStateEntryVisibilityPrivate。 主應用程式和其他命令可以呼叫該命令,但應用程式的用戶無法呼叫該命令。 在下列範例中,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();