3

I am trying to use parameter set and use default parameter set. But the default parameter set doesn't seem to work for me. Any help is much appreciated. I can easily use validate set with default action but I want to know what I am doing wrong here.

Param([cmdletbinding(DefaultParametersetname="Directory")] 
      [Parameter(Mandatory=$false,ParameterSetName="File")]
      [switch]$file, 
      [Parameter(Mandatory=$false,ParameterSetName="Directory")]  
      [switch]$directory,

[Parameter(Mandatory=$false,ParameterSetName="File")] 
[Parameter(Mandatory=$false,ParameterSetName="Directory")] 
[string]$Source,
[Parameter(Mandatory=$true,ParameterSetName="File")]  
[Parameter(Mandatory=$true,ParameterSetName="Directory")] 
[string]$DestinationPath, 
[Parameter(Mandatory=$false,ParameterSetName="Directory")] 
[Parameter(Mandatory=$false,ParameterSetName="File")]
[array]$Servers

PS C:\> Test-Script -Source "c:\somedirectory" -DestinationPath "c:\someotherdirectory"

error as shown below in the image

Test-Script : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Test-Script -Source "c:\somedirectory" -DestinationPath "c:\someotherdirectory"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Test-Script], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Test-Script

1 Answer 1

8

The CmdletBinding() attribute needs to go outside the param block, immediately before the param keyword, otherwise it will simply be ignored:

[CmdletBinding(DefaultParametersetname="Directory")]
Param(
    [Parameter(Mandatory=$false,ParameterSetName="File")]
    [switch]$file, 

    [Parameter(Mandatory=$false,ParameterSetName="Directory")]
    [switch]$directory,

    [Parameter(Mandatory=$false,ParameterSetName="File")]
    [Parameter(Mandatory=$false,ParameterSetName="Directory")]
    [string]$Source,

    [Parameter(Mandatory=$true,ParameterSetName="File")]
    [Parameter(Mandatory=$true,ParameterSetName="Directory")]
    [string]$DestinationPath,

    [Parameter(Mandatory=$false,ParameterSetName="Directory")]
    [Parameter(Mandatory=$false,ParameterSetName="File")] 
    [array]$Servers
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Mathias!! that seems to have resolved the issue.
Good catch! In my mind I was already parsing the various sets haha
@briantist something in my mind started twitching when I saw param([cmdl... ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.