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?

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.