Home > Mobile >  Levels of parameter sets for a cmdlet
Levels of parameter sets for a cmdlet

Time:03-05

What I want is to get-help to output the below for my cmdlet

SYNTAX
Get-Somehting -A <Object> [-Package <Object>] [<CommonParameters>]

Get-Somehting -A <Object> [-Names <string[]>]  [<CommonParameters>]

Get-Somehting -B <Object> [-Package <Object>] [<CommonParameters>]

Get-Somehting -B <Object> [-Names <string[]>]  [<CommonParameters>]

The following

Function Get-Somehting{
 [CmdletBinding()]
    Param(
        [Parameter(Mandatory,
        ParameterSetName='A')]
        [System.Object]$A,
        [Parameter(Mandatory,
        ParameterSetName='B')]
        [System.Object]$B,
        [Parameter(Mandatory,
        ParameterSetName='package')]
        [System.Object]$Package,
        [Parameter(Mandatory,
        ParameterSetName='Names')]
        [String[]]$Names)
    Process{
    }
 }

gives me

SYNTAX
Get-Somehting -A <Object> [-Package <Object>] [-Names <string[]>]  [<CommonParameters>]

Get-Somehting -B <Object> [-Package <Object>] [-Names <string[]>]  [<CommonParameters>]

CodePudding user response:

one of the most annoying and coolest features is the parameter set names. whats cool is you can properly define your input for the 'path' you want your code to take, and rather check what 'path' was input istead of checking all the different input parameters, however you want to be very careful when defining as pwsh REALLY wants to end up with just one invoked 'path', so you have to properly define your expected paths:

Function Get-Something {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory,ParameterSetName = 'AName')]
        [Parameter(Mandatory,ParameterSetName = 'APackage')]
        [System.Object]$A,
        [Parameter(Mandatory,ParameterSetName = 'BName')]
        [Parameter(Mandatory,ParameterSetName = 'BPackage')]
        [System.Object]$B,
        [Parameter(Mandatory,ParameterSetName = 'AName')]
        [Parameter(Mandatory,ParameterSetName = 'BName')]
        [System.Object]$Package,
        [Parameter(Mandatory,ParameterSetName = 'APackage')]
        [Parameter(Mandatory,ParameterSetName = 'BPackage')]
        [String[]]$Names)
    Process {
        $PSCmdlet.ParameterSetName
    }
}

its kind of messy, but it also makes sense as in your case you want to always have a a or b path and with that always define either names or packages, so you end up with 4 possible 'paths'

  • Related