parameter 属性を使用して、複数のパラメーター セットを宣言できます。次に、相互に排他的なパラメーターを異なるパラメーター セットに割り当てるだけです。
編集:
これは「about_Functions_Advanced_Parameters」の「ParameterSetName Named Argument」セクションにも記載されています。これは、さまざまなパラメーターのセットが次のようなコマンドレットで処理される方法ですGet-Random
(相互に排他的なパラメーターがあります)。
> get-random -input 4 -max 77
Get-Random : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:11
+ get-random <<<< -input 4 -max 77
+ CategoryInfo : InvalidArgument: (:) [Get-Random], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.GetRandomCommand
関数でそれを行う例を次に示します。
function exclusive_params() {
param(
[parameter(ParameterSetName="seta")]$one,
[parameter(ParameterSetName="setb")]$two,
$three
)
"one: $one"; "two: $two"; "three: $three"
}
パラメーターone
とtwo
は異なるパラメーター セットにあるため、一緒に指定することはできません。
> exclusive_params -one foo -two bar -three third
exclusive_params : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:17
+ exclusive_params <<<< -one foo -two bar -three third
+ CategoryInfo : InvalidArgument: (:) [exclusive_params], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,exclusive_params
これは、Get-Random で発生したのと同じエラーです。ただし、パラメーターを個別に使用できます。
> exclusive_params -one foo -three third
one: foo
two:
three: third
...また:
> exclusive_params -two bar -three third
one:
two: bar
three: third