user4872340
user4872340

Reputation:

Trouble getting parameter sets to validate

I have a function that I have been working on a while, but have shelved for a couple of months due to some frustrations with getting the parameter sets to work properly. It is meant to be part of an internal tool kit for transferring AD accounts between sites. There are several parameter sets that will be needed:

Move-AccountOut -Username <String> [<CommonParameters>]

Move-AccountOut -Username <String> [-RetainGroups] [-TransferMDrive] [-OldServer <String>] [-NewServer <String>]
[<CommonParameters>]

Move-AccountOut -Username <String> [-RetainGroups] [<CommonParameters>]

Move-AccountOut -Username <String> [-RemoveFromAllGroups] [-TransferMDrive] [-OldServer <String>] [-NewServer
<String>] [<CommonParameters>]

Move-AccountOut -Username <String> [-RemoveFromAllGroups] [<CommonParameters>]

Move-AccountOut -Username <String> [-TransferMDrive] -OldServer <String> -NewServer <String> [<CommonParameters>]

Basically, RetainGroups and RemoveFromAllGroups must be mutually exclusive. TransferMDrive could be included with either of those, or run on it's own.

Right now, I can only run RetainGroups and RemoveFromAllGroups if I include the TransferHomeDrive parameter. If I try any of those 3 on their own, I get the following:

Move-AccountOut : Parameter set cannot be resolved using the specified named parameters.
At line:1 char:1
+ Move-AccountOut -Username testuser -RetainGroups
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (:) [Move-AccountOut], ParameterBindingException
+ FullyQualifiedErrorId : AmbiguousParameterSet,Move-AccountOut

I have posted questions about issues with this function before, but I still cannot get it to resolve the parameter sets correctly. According to the get-help output above, it should work, as far as I can tell.

Here is what my param looks like:

[CmdletBinding(DefaultParameterSetName='OnlyUser')]
Param( 
    [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
    [string]$Username,

    [Parameter(ParameterSetName='RetainOnly')]
    [Parameter(ParameterSetName='RetainAndTransfer')]
    [switch]$RetainGroups,

    [Parameter(ParameterSetName='RemoveOnly')]
    [Parameter(ParameterSetName='RemoveAndTransfer')]
    [switch]$RemoveFromAllGroups,

    [Parameter(ParameterSetName='TransferMDrive')]
    [Parameter(ParameterSetName='RetainAndTransfer')]
    [Parameter(ParameterSetName='RemoveAndTransfer')]
    [switch]$TransferMDrive,

    [Parameter(ParameterSetName='TransferMDrive', Mandatory=$True)]
    [Parameter(ParameterSetName='RetainAndTransfer')]
    [Parameter(ParameterSetName='RemoveAndTransfer')]
    [string]$OldServer,

    [Parameter(ParameterSetName='TransferMDrive', Mandatory=$True)]
    [Parameter(ParameterSetName='RetainAndTransfer')]
    [Parameter(ParameterSetName='RemoveAndTransfer')]
    [string]$NewServer
)

Any advice is very much appreciated!

Upvotes: 1

Views: 91

Answers (2)

kevmar
kevmar

Reputation: 808

You may be over complicating it a bit. You are using switches for retaining or removing the groups. Can you just combine them into one switch? If so, you can cut out all the other parameter sets.

[CmdletBinding()]
Param( 
    [Parameter(Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
    [string]$Username,

    [switch]$RetainGroups,

    [Parameter(ParameterSetName='TransferMDrive')]
    [switch]$TransferMDrive,

    [Parameter(ParameterSetName='TransferMDrive', Mandatory=$True)]
    [string]$OldServer,

    [Parameter(ParameterSetName='TransferMDrive', Mandatory=$True)]
    [string]$NewServer
)

So by default it will not retain the groups and you can set the flag to keep them. Or if it needs to be the other way, flip it around.

Upvotes: 1

Keith Hill
Keith Hill

Reputation: 201632

This is how your parametersets expand out (see below). Basically given just those two parameters (Username and RetainGroups) - there are two valid parametetsets (RetainOnly and RetainAndTransfer) neither of which is the default parameter set. I think you need to make TransferMDrive mandatory for the two Transfer parametersets e.g.:

[Parameter(ParameterSetName='TransferMDrive')]
[Parameter(ParameterSetName='RetainAndTransfer', Mandatory = $True)]
[Parameter(ParameterSetName='RemoveAndTransfer', Mandatory = $True)]
[switch]$TransferMDrive,

Expanded out (original) parametersets:

    Command: Function:/Move-AccountOut
    Set:    OnlyUser *


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    RetainAndTransfer


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
NewServer              {N*}         Named    False     False    False  All     
OldServer              {Ol*}        Named    False     False    False  All     
RetainGroups           {Ret*}       Named    False     False    False  All     
TransferMDrive         {T*}         Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    RetainOnly


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
RetainGroups           {Ret*}       Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    RemoveAndTransfer


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
NewServer              {N*}         Named    False     False    False  All     
OldServer              {Ol*}        Named    False     False    False  All     
RemoveFromAllGroups    {Rem*}       Named    False     False    False  All     
TransferMDrive         {T*}         Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    RemoveOnly


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
RemoveFromAllGroups    {Rem*}       Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     


    Command: Function:/Move-AccountOut
    Set:    TransferMDrive


Name                   Aliases      Position Mandatory Pipeline ByName Provider
----                   -------      -------- --------- -------- ------ --------
InformationAction      {infa}       Named    False     False    False  All     
InformationVariable    {iv}         Named    False     False    False  All     
NewServer              {N*}         Named    True      False    False  All     
OldServer              {Ol*}        Named    True      False    False  All     
TransferMDrive         {T*}         Named    False     False    False  All     
Username               {U*}         Named    True      True     True   All     

Upvotes: 1

Related Questions