2011-08-23 8 views

답변

27

이 같은 각 매개 변수 위의 속성에이를 지정 :

function Do-Something{ 
    [CmdletBinding()] 
    param(
     [Parameter(Position=0,mandatory=$true)] 
     [string] $aMandatoryParam, 
     [Parameter(Position=1,mandatory=$true)] 
     [string] $anotherMandatoryParam) 

    process{ 
     ... 
    } 
} 
9

매개 변수 설명에 "사실 필수 = $"는 추가 매개 변수가 필수하려면. 선택적 매개 변수를 만들려면 "필수"문을 밖으로 둡니다.

이 코드는 모두 스크립트 기능 매개 변수에 대한 작동합니다

[CmdletBinding()] 
param(
    [Parameter(Mandatory=$true)] 
    [String]$aMandatoryParameter, 

    [String]$nonMandatoryParameter, 

    [Parameter(Mandatory=$true)] 
    [String]$anotherMandatoryParameter 

) 

는 "PARAM"문은 스크립트 나 기능 중 하나에서 (명령과 빈 줄을 제외하고) 첫 번째 있는지 확인합니다.

당신은 매개 변수를 확인하기 위해 "GET-도움말"cmdlet를 사용할 수 있습니다

가 올바르게 정의되었습니다

PS C:\> get-help Script.ps1 -full 
[...] 
PARAMETERS 
    -aMandatoryParameter <String> 

     Required?     true 
     Position?     1 
     Default value 
     Accept pipeline input?  false 
     Accept wildcard characters? 

    -NonMandatoryParameter <String> 

     Required?     false 
     Position?     2 
     Default value 
     Accept pipeline input?  false 
     Accept wildcard characters? 

    -anotherMandatoryParameter <String> 

     Required?     true 
     Position?     3 
     Default value 
     Accept pipeline input?  false 
     Accept wildcard characters? 
+0

그냥 궁금해서, 무슨 일 때 .BAT 왼쪽 된이 .ps1 및 필수 매개 변수를 호출 말할 수 아웃. PS1 응답/오류는 어떻게 생겼습니까? – Quanda

+0

필수 매개 변수를 제공하지 않고 PowerShell 스크립트를 호출하면 스크립트가 실행하기 전에 대화 형으로 대화 상자를 표시합니다. "다음 매개 변수에 대한 값을 제공하십시오. aMandatoryParameter :" – llmora

관련 문제