cmdlet을 디자인할 때 동일한 데이터 조각에서 여러 작업을 수행해야 하는 경우가 발생할 수 있습니다. 예를 들어 데이터를 가져와서 설정하거나 프로세스를 시작하고 중지해야 할 수 있습니다. 각 작업을 수행하려면 별도의 cmdlet을 만들어야 하지만 cmdlet 디자인에는 개별 cmdlet에 대한 클래스가 파생되는 기본 클래스가 포함되어야 합니다.
기본 클래스를 구현할 때는 다음 사항에 유의하세요.
기본 클래스의 모든 파생 cmdlet에서 사용하는 공통 매개 변수를 선언합니다.
적절한 cmdlet 클래스에 cmdlet 관련 매개 변수를 추가합니다.
기본 클래스에서 적절한 입력 처리 메서드를 재정의합니다.
모든 cmdlet 클래스에서 System.Management.Automation.CmdletAttribute 특성을 선언하지만 기본 클래스에서는 선언하지 않습니다.
이름 및 설명이 cmdlet 집합을 반영하는 system.Management.Automation.PSSnapIn 또는 System.Management.Automation.CustomPSSnapIn 클래스를 구현합니다.
예시
다음 예제에서는 동일한 기본 클래스에서 파생되는 Get-Proc 및 Stop-Proc cmdlet에서 사용하는 기본 클래스의 구현을 보여 주세요.
using System;
using System.Diagnostics;
using System.Management.Automation; //Windows PowerShell namespace.
namespace Microsoft.Samples.PowerShell.Commands
{
#region ProcessCommands
/// <summary>
/// This class implements a Stop-Proc cmdlet. The parameters
/// for this cmdlet are defined by the BaseProcCommand class.
/// </summary>
[Cmdlet(VerbsLifecycle.Stop, "Proc", SupportsShouldProcess = true)]
public class StopProcCommand : BaseProcCommand
{
public override void ProcessObject(Process process)
{
if (ShouldProcess(process.ProcessName, "Stop-Proc"))
{
process.Kill();
}
}
}
/// <summary>
/// This class implements a Get-Proc cmdlet. The parameters
/// for this cmdlet are defined by the BaseProcCommand class.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Proc")]
public class GetProcCommand : BaseProcCommand
{
public override void ProcessObject(Process process)
{
WriteObject(process);
}
}
/// <summary>
/// This class is the base class that defines the common
/// functionality used by the Get-Proc and Stop-Proc
/// cmdlets.
/// </summary>
public class BaseProcCommand : Cmdlet
{
#region Parameters
// Defines the Name parameter that is used to
// specify a process by its name.
[Parameter(
Position = 0,
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true
)]
public string[] Name
{
get { return processNames; }
set { processNames = value; }
}
private string[] processNames;
// Defines the Exclude parameter that is used to
// specify which processes should be excluded when
// the cmdlet performs its action.
[Parameter()]
public string[] Exclude
{
get { return excludeNames; }
set { excludeNames = value; }
}
private string[] excludeNames = new string[0];
#endregion Parameters
public virtual void ProcessObject(Process process)
{
throw new NotImplementedException("This method should be overridden.");
}
#region Cmdlet Overrides
// <summary>
// For each of the requested process names, retrieve and write
// the associated processes.
// </summary>
protected override void ProcessRecord()
{
// Set up the wildcard characters used in resolving
// the process names.
WildcardOptions options = WildcardOptions.IgnoreCase |
WildcardOptions.Compiled;
WildcardPattern[] include = new WildcardPattern[Name.Length];
for (int i = 0; i < Name.Length; i++)
{
include[i] = new WildcardPattern(Name[i], options);
}
WildcardPattern[] exclude = new WildcardPattern[Exclude.Length];
for (int i = 0; i < Exclude.Length; i++)
{
exclude[i] = new WildcardPattern(Exclude[i], options);
}
foreach (Process p in Process.GetProcesses())
{
foreach (WildcardPattern wIn in include)
{
if (wIn.IsMatch(p.ProcessName))
{
bool processThisOne = true;
foreach (WildcardPattern wOut in exclude)
{
if (wOut.IsMatch(p.ProcessName))
{
processThisOne = false;
break;
}
}
if (processThisOne)
{
ProcessObject(p);
}
break;
}
}
}
}
#endregion Cmdlet Overrides
}
#endregion ProcessCommands
}
또한 참조하십시오
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
PowerShell