2

I have a function worked out that has 2 parameter sets, both of which contain mandatory parameters. I would like to design the function so that the sets can each be used alone, or together. This is what I have put together:

function Do-This {
    [CmdletBinding(DefaultParameterSetName='NoTransferOrCopy')]
    Param(
        [Parameter(ParameterSetName='CopyPermissions')]
        [Parameter(ParameterSetName='CopyAndTransfer')]
        [switch]$CopyPermissions,

        [Parameter(ParameterSetName='CopyPermissions', Mandatory=$True)]
        [Parameter(ParameterSetName='CopyAndTransfer', Mandatory=$True)]
        [string]$FromUser,

        [Parameter(ParameterSetName='CopyPermissions')]
        [Parameter(ParameterSetName='CopyAndTransfer')]
        [switch]$CopyAll,

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

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

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

The only issue that I have is that for example, if I specify -CopyPermissionsby itself on the command line, I get an ambiguous parameter set message instead of being prompted for a value for FromUser.

I understand why this is occurring, however if possible I would like to throw an error that is more descriptive, something like FromUser must be specified whenever CopyPermissions is set. Is there any way I can do that?

  • Take a look at this question: [Using Advanced Parameter Sets in a Function with Multiple Unique Switches](http://superuser.com/questions/929743/using-advanced-parameter-sets-in-a-function-with-multiple-unique-switches) – beatcracker Aug 11 '15 at 18:34

0 Answers0