1

Can someone tell what I am doing wrong in the below I wrote:

function set-harden {
[CmdletBinding(DefaultParameterSetName='NormalHardening')]
param (
[Parameter(ParameterSetName='DoNotRemoveFromDomain')]
[Parameter(ParameterSetName='PermitHTTP' ,Mandatory=$True)]
[Parameter(ParameterSetName='PermitHTTPS' ,Mandatory=$True)]
            [switch]$DONOTRemovefromdomain,

[Parameter(ParameterSetName='PermitHTTP')]
[Parameter(ParameterSetName='DoNotRemoveFromDomain')]
            [switch]$Permithttp,

[Parameter(ParameterSetName='PermitHTTPS')]
[Parameter(ParameterSetName='DoNotRemoveFromDomain')]
        [switch]$Permithttps,

[Parameter(ParameterSetName='NormalHardening')]
            $NormalHardening
 )}

 If($NormalHardening -eq ""){
    Write-Host "Excellent!"
 }

All I want to do is to let the user select -DONOTRemovefromdomain or -Permithttp or even -Permithttps. There could be a variety of options the user has to choose from.

When I run this below I get an error:

PS C:\Temp> set-harden -DONOTRemovefromdomain -Permithttp
set-harden : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ set-harden -DONOTRemovefromdomain -Permithttp
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [set-harden], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,set-harden

Also, if I do not specify anything (so it should just go to the parameter NormalHardening) I get an nothing back:

PS C:\Temp> set-harden

PS C:\Temp> 
briantist
  • 45,546
  • 6
  • 82
  • 127
lara400
  • 4,646
  • 13
  • 46
  • 66

2 Answers2

3

You've specified two flags, DONOTRemovefromDomain and Permithttp that belong to two parameter sets, DoNotRemoveFromDomain and PermitHttp. The command parser has no way of knowing which parameter set you mean, so you get an error.

The reason you don't get an error when you don't specify anything is because you've set the default parameter set explicitly to NormalHardening. You've not set the Mandatory flag on the single parameter in this parameter set, and by default parameters are not mandatory so you're not seeing an error.

Instead of having all these parameter sets why not just have 2, one for the default and one for all the flags you want to set:

function set-harden {
[CmdletBinding(DefaultParameterSetName='NormalHardening')]
param (
[Parameter(ParameterSetName='Options')]
[switch]$DONOTRemovefromdomain,

[Parameter(ParameterSetName='Options')]
[switch]$Permithttp,

[Parameter(ParameterSetName='Options')]
[switch]$Permithttps,

[Parameter(ParameterSetName='NormalHardening')]
$NormalHardening
 )}

 If($PSCmdlet.ParameterSetName -eq "Options"){
    Write-Host "Excellent!"
 }

How, if the parameter set name is set to Options you can check and apply the flags. If it's set to NormalHarding then you know to use the $NormalHardening parameter.

Sean
  • 60,939
  • 11
  • 97
  • 136
  • Thanks for that. Editing my comment based on your update.. I think I tried your solution earlier in the the day but for some reason I was not aware able to select multiple options.. I will try yours anyway as I probably made a mistake on mine – lara400 May 06 '16 at 14:27
  • @lara400 - one options might be to split this out into multiple scripts instead of trying to cram all the functionality into one script. – Sean May 06 '16 at 14:28
  • @lara400 when using a switch parameter that you want in a certain parameter set but not in another, make it mandatory in one, then leave it out of the other completely (don't make it optional). It would help if you mapped out all of the combinations you wanted. – briantist May 06 '16 at 14:28
2

Sean gave a good answer already about what's going on in your specific case, but I want to include some tips for troubleshooting parameter sets.

Get Help

Or more specifically, Get-Help. The parameter set syntax is automatically generated from the param block, so running Get-Help myFunction will show you how PowerShell is interpreting your parameter sets (how many, which parameters are mandatory or not in each set, etc.).

Trace the Call

If the sets look right but you're getting errors and aren't sure why, let PowerShell show you how it's binding parameters:

Trace-Command -Name ParameterBinding -Expression { Set-Harden -Permithttp } -PSHost

That can give you great insight on what's going on, and lead you to how you might fix that (or help you realize that you can't).

Community
  • 1
  • 1
briantist
  • 45,546
  • 6
  • 82
  • 127