성능 또는 보안상의 이유로 호스트 애플리케이션에서 사용할 수 있는 Windows PowerShell 명령을 제한할 수 있습니다. 이렇게 하려면 System.Management.Automation.Runspaces.InitialSessionState.Create* 메서드를 호출하여 빈 System.Management.Automation.Runspaces.InitialSessionState 만든 다음 사용 가능한 명령만 추가합니다.
지정한 명령만 로드하는 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.SessionStateEntryVisibility Private설정하여 명령을 비공개로 설정할 수도 있습니다. 호스트 애플리케이션 및 기타 명령은 해당 명령을 호출할 수 있지만 애플리케이션 사용자는 호출할 수 없습니다. 다음 예제에서 Get-ChildItem 명령은 private입니다.
defaultSessionState = InitialSessionState.CreateDefault();
commandIndex = GetIndexOfEntry(defaultSessionState.Commands, "Get-ChildItem");
defaultSessionState.Commands[commandIndex].Visibility = SessionStateEntryVisibility.Private;
this.runspace = RunspaceFactory.CreateRunspace(defaultSessionState);
this.runspace.Open();
또한 참조하십시오
PowerShell