
다음 콘텐츠가 포함된 .ps1이 있다고 가정해 보겠습니다(실제 콘텐츠는 중요하지 않음).
param(
[Parameter(Mandatory=$true)]
[string]
$someArgument
)
# Do something risky
Remove-Item "c:\path\$someArgument.txt"
내 스크립트(또는 실제로 모든 메소드)에 확인 메시지를 추가하고 싶습니다. 이 메시지는 기본적으로 끌 수 있습니다.
.\myscript.ps1 "foo" -Confirm:$false
어떻게 해야 합니까?
답변1
고급 기능을 사용하면 ShouldProcess라는 기능을 사용할 수 있습니다.
스크립트 상단에 [CmdletBinding]이라는 특성을 추가할 수 있습니다.
[CmdletBinding(
SupportsShouldProcess=$true,
ConfirmImpact="High"
)]
그런 다음 함수 본문에서 if 문을 사용해야 합니다.
if ($pscmdlet.ShouldProcess($thingIamGoingToChange))
about_advanced_functions 도움말과 about_functions_cmdletbind 도움말을 살펴보세요.