मान लें कि मेरे पास यह फ़ंक्शन है:
function Foo{
[CmdLetBinding()]
param(
[Parameter(Mandatory=$true,ParameterSetName="A",Position=0)]
[Parameter(Mandatory=$true,ParameterSetName="both",Position=0)]
[int]
$A,
[Parameter(Mandatory=$true,ParameterSetName="B",Position=0)]
[Parameter(Mandatory=$true,ParameterSetName="both",Position=1)]
[int]
$B
)
Write-Host $PsCmdlet.ParameterSetName
}
मुझे उम्मीद है कि यह विधि ए, या बी या दोनों की अपेक्षा करेगी। कभी कोई नहीं।
हालाँकि, अगर मैं इसे कॉल करता हूं:
Foo -A 1 -B 2 # outputs "both" as expected
Foo -B 3 # error
Foo -A 4 # error
मुझे जो त्रुटि मिलती है वह है:
Foo : Parameter set cannot be resolved using the specified named parameters.
At c:\pathto:75 char:4
+ Foo <<<< -B 3
+ CategoryInfo : InvalidArgument: (:) [Foo], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Foo
Foo : Parameter set cannot be resolved using the specified named parameters.
At c:\pathto.ps1:76 char:4
+ Foo <<<< -A 4
+ CategoryInfo : InvalidArgument: (:) [Foo], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Foo
मैं परेशान था, क्योंकि जैसा कि आप देख सकते हैं, मैं स्पष्ट रूप से मापदंडों का नाम निर्दिष्ट करता हूं।
मुझे अपेक्षा के अनुसार काम करने के लिए अपना फ़ंक्शन कैसे लिखना चाहिए?
ParameterSetName="__AllParameterSets"