확인을 요청하는 방법
이 예제에서는 작업을 수행하기 전에 System.Management.Automation.Cmdlet.ShouldProcess 및 System.Management.Automation.Cmdlet.ShouldContinue 메서드를 호출하여 사용자에게 확인을 요청하는 방법을 보여 드립니다.
중요
Windows PowerShell 이러한 요청을 처리하는 방법에 대한 자세한 내용은 확인 요청을 참조하세요.
확인을 요청하려면
SupportsShouldProcess
Cmdlet 특성의 매개 변수가 로 설정되어 있는지true
확인합니다. 함수의 경우 CmdletBinding 특성의 매개 변수입니다.[Cmdlet(VerbsDiagnostic.Test, "RequestConfirmationTemplate1", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
참고
SupportsShouldProcess
를 단독으로 사용한다고 해서 사용자에게 확인 메시지가 표시된다는 보장은 없습니다. 프롬프트는 의$ConfirmPreference
값과 작업의 영향에 따라 결정됩니다. 를 사용하여ConfirmImpact
작업의 영향 심각도를 설정합니다.사용자가
Force
확인 요청을 재정의할 수 있도록 cmdlet에 매개 변수를 추가합니다.[Parameter()] public SwitchParameter Force { get { return force; } set { force = value; } } private bool force;
if
System.Management.Automation.Cmdlet.ShouldProcess 메서드의 반환 값을 사용하여 System.Management.Automation.Cmdlet.ShouldContinue 메서드가 호출되는지 확인하는 문을 추가합니다.if
System.Management.Automation.Cmdlet.ShouldContinue 메서드의 반환 값과 매개 변수 값을 사용하여 작업을 수행해야 하는지 여부를 결정하는 두 번째 문을Force
추가합니다.
예제
다음 코드 예제에서는 System.Management.Automation.Cmdlet.ShouldProcess 및 System.Management.Automation.Cmdlet.ShouldContinue 메서드가 System.Management.Automation.Cmdlet.ProcessRecord 메서드의 재정의 내에서 호출됩니다. 그러나 다른 입력 처리 메서드에서 이러한 메서드를 호출할 수도 있습니다.
protected override void ProcessRecord()
{
if (ShouldProcess("ShouldProcess target"))
{
if (Force || ShouldContinue("", ""))
{
// Add code that performs the operation.
}
}
}
관련 항목
Writing a Windows PowerShell Cmdlet(Windows PowerShell Cmdlet 작성)
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기