Cmdlet을 작성 하는 방법
이 문서에서는 cmdlet을 작성 하는 방법을 보여 줍니다. Send-Greeting
Cmdlet은 단일 사용자 이름을 입력으로 사용 하 고 해당 사용자에 게 인사말을 작성 합니다. Cmdlet은 많은 작업을 수행 하지 않지만이 예에서는 cmdlet의 주요 섹션을 보여 줍니다.
Cmdlet을 작성 하는 단계
클래스를 cmdlet으로 선언 하려면 cmdlet 특성을 사용 합니다. Cmdlet 특성은 cmdlet 이름에 대 한 동사와 명사를 지정 합니다.
Cmdlet 특성에 대 한 자세한 내용은 cmdletattribute 선언을 참조 하세요.
클래스의 이름을 지정 합니다.
다음 클래스 중 하나에서 cmdlet이 파생 되도록 지정 합니다.
Cmdlet에 대 한 매개 변수를 정의 하려면 매개 변수 특성을 사용 합니다. 이 경우 필수 매개 변수를 하나만 지정 합니다.
Parameter 특성에 대 한 자세한 내용은 parameterattribute 선언을 참조 하세요.
입력을 처리 하는 입력 처리 메서드를 재정의 합니다. 이 경우에는 ProcessRecord 메서드가 재정의 되 고,
인사말을 작성 하려면 WriteObject메서드를 사용 합니다. 인사말이 다음과 같은 형식으로 표시 됩니다.
Hello <UserName>!
예제
using System.Management.Automation; // Windows PowerShell assembly.
namespace SendGreeting
{
// Declare the class as a cmdlet and specify the
// appropriate verb and noun for the cmdlet name.
[Cmdlet(VerbsCommunications.Send, "Greeting")]
public class SendGreetingCommand : Cmdlet
{
// Declare the parameters for the cmdlet.
[Parameter(Mandatory=true)]
public string Name
{
get { return name; }
set { name = value; }
}
private string name;
// 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 + "!");
}
}
}
참고 항목
Writing a Windows PowerShell Cmdlet(Windows PowerShell Cmdlet 작성)
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기