이 샘플에서는 기본 초기 세션 상태를 만드는 방법, System.Management.Automation.Runspaces.InitialSessionStatecmdlet을 추가하는 방법, 초기 세션 상태를 사용하는 Runspace를 만드는 방법 및 System.Management.Automation.PowerShell 개체를 사용하여 명령을 실행하는 방법을 보여 줍니다.
요구 사항
이 샘플에는 Windows PowerShell 2.0이 필요합니다.
입증합니다
이 샘플에서는 다음을 보여 줍니다.
System.Management.Automation.Runspaces.InitialSessionState 개체 만들기
System.Management.Automation.Runspaces.InitialSessionState 개체에 cmdlet(호스트 애플리케이션에서 정의)을 추가합니다.
개체를 사용하는 System.Management.Automation.Runspaces.Runspace 개체를 만듭니다.
System.Management.Automation.Runspaces.Runspace 개체를 사용하는 System.Management.Automation.PowerShell 개체를 만듭니다.
System.Management.Automation.PowerShell 개체의 파이프라인에 명령을 추가합니다.
명령에서 반환된 System.Management.Automation.PSObject 개체에서 속성을 추출합니다.
예시
이 샘플에서는 System.Management.Automation.Runspaces.InitialSessionState 개체를 사용하여 runspace를 열 때 사용할 수 있는 요소를 정의하는 runspace를 만듭니다. 이 샘플에서는 호스트 애플리케이션에서 정의한 Get-Proc cmdlet이 초기 세션 상태에 추가되고 System.Management.Automation.PowerShell 개체를 사용하여 cmdlet이 동기적으로 실행됩니다.
namespace Microsoft.Samples.PowerShell.Runspaces
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using PowerShell = System.Management.Automation.PowerShell;
#region GetProcCommand
/// <summary>
/// Class that implements the GetProcCommand.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : Cmdlet
{
#region Cmdlet Overrides
/// <summary>
/// For each of the requested process names, retrieve and write
/// the associated processes.
/// </summary>
protected override void ProcessRecord()
{
// Get the current processes.
Process[] processes = Process.GetProcesses();
// Write the processes to the pipeline making them available
// to the next cmdlet. The second argument (true) tells the
// system to enumerate the array, and send one process object
// at a time to the pipeline.
WriteObject(processes, true);
}
#endregion Overrides
} // End GetProcCommand class.
#endregion GetProcCommand
/// <summary>
/// This class contains the Main entry point for this host application.
/// </summary>
internal class Runspace10
{
/// <summary>
/// This sample shows how to create a default initial session state, how to add
/// add a cmdlet to the InitialSessionState object, and then how to create
/// a Runspace object.
/// </summary>
/// <param name="args">Parameter is not used.</param>
/// This sample demonstrates:
/// 1. Creating an InitialSessionState object.
/// 2. Adding a cmdlet to the InitialSessionState object.
/// 3. Creating a runspace that uses the InitialSessionState object.
/// 4. Creating a PowerShell object that uses the Runspace object.
/// 5. Running the added command synchronously.
/// 6. Working with PSObject objects to extract properties
/// from the objects returned by the pipeline.
private static void Main(string[] args)
{
// Create a default InitialSessionState object. The default
// InitialSessionState object contains all the elements provided
// by Windows PowerShell.
InitialSessionState iss = InitialSessionState.CreateDefault();
// Add the Get-Proc cmdlet to the InitialSessionState object.
SessionStateCmdletEntry ssce = new SessionStateCmdletEntry("Get-Proc", typeof(GetProcCommand), null);
iss.Commands.Add(ssce);
// Create a Runspace object that uses the InitialSessionState object.
// Notice that no PSHost object is specified, so the default host is used.
// See the Hosting samples for information on creating your own custom host.
using (Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss))
{
myRunSpace.Open();
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = myRunSpace;
// Add the Get-Proc cmdlet to the pipeline of the PowerShell object.
powershell.AddCommand("Get-Proc");
Collection<PSObject> results = powershell.Invoke();
Console.WriteLine("Process HandleCount");
Console.WriteLine("--------------------------------");
// Display the output of the pipeline.
foreach (PSObject result in results)
{
Console.WriteLine(
"{0,-20} {1}",
result.Members["ProcessName"].Value,
result.Members["HandleCount"].Value);
}
}
// Close the runspace to release resources.
myRunSpace.Close();
}
System.Console.WriteLine("Hit any key to exit...");
System.Console.ReadKey();
}
}
}
또한 참조하십시오
PowerShell