Parameter sets do not seem to be working in my function - help please?

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of I Am Sir Ask Alot I Am Sir Ask Alot 1 year, 9 months ago.

  • Author
    Posts
  • #28639
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    What I want to do is to not be able to use the -Equals and -Matches parameters together, but it is not working.

    How can I make them mutually exclusive here using parameter sets?

    Here is an example of what I have:

    Function Move-Something
    {
    	[cmdletbinding()]
    	
    	Param
    	(
    		[Parameter(Position = 0, Mandatory = $true,)]
    		[ValidateNotNullOrEmpty()]
    		[Alias('CN')]
    		$ComputerName,
    		
    		[Parameter(Position = 1, Mandatory = $true)]
    		[ValidateNotNullOrEmpty()]
    		[string]$MoveFrom,
    		
    		[Parameter(Position = 2, Mandatory = $true,)]
                    [ValidateNotNullOrEmpty()]
    		[string]$MoveTo,
    		
    		[Parameter(Postion=3, ParameterSetName = 'Equals')]
    		[switch]$Equals,
    		
    		[Parameter(Position=3, ParameterSetName = 'Matches')]
    		[switch]$Matches
    	)
    
    Begin {}
    
    Process {}
    
    End {}
    
    }
    

    Thank you all

  • #28640
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Your Parameter sets are correct, but you have several type-os that are breaking the function. On the Parameters in Position 0 and 2, you need to remove the commas after the Mandatory = $true , and in Position 3 'Equals', you need to spell Position correctly. After that it should work just fine.

    Function Move-Something
    {
    	[cmdletbinding()]
    	
    	Param
    	(
    		[Parameter(Position = 0, Mandatory = $true)]
    		[ValidateNotNullOrEmpty()]
    		[Alias('CN')]
    		$ComputerName,
    		
    		[Parameter(Position = 1, Mandatory = $true)]
    		[ValidateNotNullOrEmpty()]
    		[string]$MoveFrom,
    		
    		[Parameter(Position = 2, Mandatory = $true)]
                    [ValidateNotNullOrEmpty()]
    		[string]$MoveTo,
    		
    		[Parameter(Position=3, ParameterSetName = 'Equals')]
    		[switch]$Equals,
    		
    		[Parameter(Position=3, ParameterSetName = 'Matches')]
    		[switch]$Matches
    	)
    
    Begin {}
    
    Process {}
    
    End {}
    
    }
    
  • #28641
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    There were a lot of small things wrong like commas after the first two Mandatory entries and Position misspelled in the Equals switch. Start with this:

    Function Move-Something
    {
    	[cmdletbinding()]
    	
    	Param (
            [Parameter(Position=0,Mandatory=$true)]
    		[ValidateNotNullOrEmpty()]
    		[Alias('CN')]
    		$ComputerName,
    		
    		[Parameter(Position=1,Mandatory=$true)]
    		[ValidateNotNullOrEmpty()]
    		[string]$MoveFrom,
    		
    		[Parameter(Position=2,Mandatory=$true)]
            [ValidateNotNullOrEmpty()]
    		[string]$MoveTo,
    		
    		[Parameter(Position=3,ParameterSetName='Equals')]
    		[switch]$Equals,
    		
    		[Parameter(Position=4,ParameterSetName='Matches')]
    		[switch]$Matches
    	)
    
    Begin {}
    
    Process {}
    
    End {}
    }
    

    This command works:

    Move-Something -ComputerName Test -MoveTo C:\Test -MoveFrom C:\Temp -Equals
    

    If we pass both parameters or don't include either switch:

    PS C:\Users\Rob> Move-Something -ComputerName Test -MoveTo C:\Test -MoveFrom C:\Temp -Equals -Matches
    Move-Something : Parameter set cannot be resolved using the specified named parameters.
    At line:1 char:1
    + Move-Something -ComputerName Test -MoveTo C:\Test -MoveFrom C:\Temp -Equals -Mat ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Move-Something], ParameterBindingException
        + FullyQualifiedErrorId : AmbiguousParameterSet,Move-Something
     
    
    PS C:\Users\Rob> Move-Something -ComputerName Test -MoveTo C:\Test -MoveFrom C:\Temp
    Move-Something : Parameter set cannot be resolved using the specified named parameters.
    At line:1 char:1
    + Move-Something -ComputerName Test -MoveTo C:\Test -MoveFrom C:\Temp
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Move-Something], ParameterBindingException
        + FullyQualifiedErrorId : AmbiguousParameterSet,Move-Something
    

    It appears to be working as expected even though the error is a bit cryptic.

  • #28642
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Ahh geez, my bad. In my original function there are no typos and there are no comma issues. Sorry, I typed this up in haste. What prompted me to ask this question is because when I begin typing up my function in my PowerShell Studio editor and when I use -Equals, the intelli-sense feature still shows that I can use the -Matches parameter, but I have not actually ran the script. It is not fully developed yet. So I guess like Rob pointed out, it is working.

    Thanks

  • #28644
    Profile photo of Don Jones
    Don Jones
    Keymaster

    PowerShell Studio probably attempts to do a static analysis of the code to determine what parameters are available, but may not pay attention to things like parameter sets. I wouldn't rely on Intellisense (even in the ISE) to test if it's working :). Glad it's working now!

  • #28645
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Great info, Don.

    Thanks

You must be logged in to reply to this topic.