Author Posts

August 14, 2015 at 4:12 pm

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

August 14, 2015 at 4:34 pm

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 {}

}

August 14, 2015 at 4:59 pm

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.

August 14, 2015 at 9:45 pm

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

August 15, 2015 at 6:57 am

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!

August 15, 2015 at 11:38 am

Great info, Don.

Thanks