Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: parameter: how to require either one or the other but not both

$
0
0

You can do this using parameter sets. By putting each parameter in one set and not the other, PowerShell will take care of the rest. You may want to define a default parameter set, as well:

function YourCmdletName
{
    [CmdletBinding(DefaultParameterSetName='ByUserName')]
    param (
        [Parameter(Mandatory = $true, ParameterSetName = 'ByUserName')]
        [System.String]
        $UserName,

        [Parameter(Mandatory = $true, ParameterSetName = 'ByEmployeeId')]
        [System.String]
        $EmployeeId
    )

    # In the function's code, you can either test for whether $UserName and/or $EmployeedId are null, or
    # check $PSBoundParameters.ContainsKey('UserName'), or check $PSCmdlet.ParameterSetName to see whether it
    # is set to 'ByUserName' or 'ByEmployeeId'.
}


Viewing all articles
Browse latest Browse all 13067

Trending Articles