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

Welcome Forums General PowerShell Q&A Parameter sets do not seem to be working in my function – help please?

Viewing 5 reply threads
  • Author
    Posts
    • #28639
      Participant
      Topics: 44
      Replies: 79
      Points: 57
      Rank: Member

      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
      Participant
      Topics: 6
      Replies: 667
      Points: 97
      Helping Hand
      Rank: Member

      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
      Participant
      Topics: 12
      Replies: 1618
      Points: 2,543
      Helping Hand
      Rank: Community Hero

      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
      Participant
      Topics: 44
      Replies: 79
      Points: 57
      Rank: Member

      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
      Keymaster
      Topics: 18
      Replies: 4872
      Points: 1,903
      Helping HandTeam Member
      Rank: Community Hero

      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
      Participant
      Topics: 44
      Replies: 79
      Points: 57
      Rank: Member

      Great info, Don.

      Thanks

Viewing 5 reply threads
  • The topic ‘Parameter sets do not seem to be working in my function – help please?’ is closed to new replies.