이 예제에서는 다른 이진 cmdlet 내에서 직접 [System.Management.Automation.Cmdlet]
파생되는 이진 cmdlet을 호출하는 방법을 보여 줍니다. 이를 통해 호출된 cmdlet의 기능을 개발 중인 이진 cmdlet에 추가할 수 있습니다. 이 예제에서는 로컬 컴퓨터에서 실행 중인 프로세스를 가져오기 위해 Get-Process
cmdlet이 호출됩니다.
Get-Process
cmdlet에 대한 호출은 다음 명령과 동일합니다. 이 명령은 이름이 "a"에서 "t"로 시작하는 모든 프로세스를 검색합니다.
Get-Process -Name [a-t]*
중요합니다
System.Management.Automation.Cmdlet 클래스에서 직접 파생되는 cmdlet만 호출할 수 있습니다. System.Management.Automation.PSCmdlet 클래스에서 파생되는 cmdlet은 호출할 수 없습니다. 예제는 PSCmdlet내에서 PSCmdlet을 호출하는 방법을 참조하세요.
cmdlet 내에서 cmdlet을 호출하려면
호출할 cmdlet을 정의하는 어셈블리가 참조되고 적절한
using
문이 추가되었는지 확인합니다. 이 예제에서는 다음 네임스페이스가 추가됩니다.using System.Diagnostics; using System.Management.Automation; // PowerShell assembly. using Microsoft.PowerShell.Commands; // PowerShell cmdlets assembly you want to call.
cmdlet의 입력 처리 메서드에서 호출할 cmdlet의 새 인스턴스를 만듭니다. 이 예제에서는 microsoft.PowerShell.Commands.GetProcessCommand 형식의 개체가 cmdlet을 호출할 때 사용되는 인수를 포함하는 문자열과 함께 만들어집니다.
GetProcessCommand gp = new GetProcessCommand(); gp.Name = new string[] { "[a-t]*" };
System.Management.Automation.Cmdlet.Invoke* 메서드를 호출하여
Get-Process
cmdlet을 호출합니다.foreach (Process p in gp.Invoke<Process>()) { Console.WriteLine(p.ToString()); } }
예시
이 예제에서 Get-Process
cmdlet은 cmdlet의 System.Management.Automation.Cmdlet.BeginProcessing 메서드 내에서 호출됩니다.
using System;
using System.Diagnostics;
using System.Management.Automation; // PowerShell assembly.
using Microsoft.PowerShell.Commands; // PowerShell cmdlets assembly you want to call.
namespace SendGreeting
{
// Declare the class as a cmdlet and specify an
// appropriate verb and noun for the cmdlet name.
[Cmdlet(VerbsCommunications.Send, "GreetingInvoke")]
public class SendGreetingInvokeCommand : Cmdlet
{
// Declare the parameters for the cmdlet.
[Parameter(Mandatory = true)]
public string Name { get; set; }
// Override the BeginProcessing method to invoke
// the Get-Process cmdlet.
protected override void BeginProcessing()
{
GetProcessCommand gp = new GetProcessCommand();
gp.Name = new string[] { "[a-t]*" };
foreach (Process p in gp.Invoke<Process>())
{
WriteVerbose(p.ToString());
}
}
// Override the ProcessRecord method to process
// the supplied user name and write out a
// greeting to the user by calling the WriteObject
// method.
protected override void ProcessRecord()
{
WriteObject("Hello " + Name + "!");
}
}
}
또한 참조하십시오
PowerShell