如何定义不需要参数的参数集 (PS cmdlet)?

How to define a Parameter Set in which no parameter is required(PS cmdlet)?

我要定义三个参数集

  1. cmdletName - param1 (ParameterSet1)
  2. cmdletName - param1 -param2 (ParameterSet2)
  3. cmdletName(这个没有任何参数)(ParameterSet3)

我该怎么做?

这是我当前代码的样子。

[Parameter(ParameterSetName = ParameterSet1, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The ResourceGroupName")]
    [Parameter(ParameterSetName = ParameterSet2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The ResourceGroupName")]
    [Parameter(ParameterSetName = ParameterSet3, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The ResourceGroupName")]
    public String ResourceGroupName { get; set; }

[Parameter(ParameterSetName = ParameterSet2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of metric alert rule")]
    [Parameter(ParameterSetName = ParameterSet3, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The ResourceGroupName")]
    public String Name { get; set; }

I want to define three parameter sets

  • cmdletName -param1         (ParameterSet1)
  • cmdletName -param1 -param2 (ParameterSet2)
  • cmdletName (this does not have any parameter)(ParameterSet3)

How can I do this?

这是可能的评论 Powershell 解决方案,带有一些调试输出:

function cmdletName{
param(
    [parameter( Mandatory=$true, ParameterSetName="ParameterSet1",
                ValueFromPipelineByPropertyName=$true)]
    [parameter( Mandatory=$true, ParameterSetName="ParameterSet2",
                ValueFromPipelineByPropertyName=$true)]
    ### [ValidateNotNullOrEmpty()]
    [string]$param1,

    [parameter( Mandatory=$true, ParameterSetName="ParameterSet2", 
                ValueFromPipelineByPropertyName=$true)]
    ### [ValidateNotNullOrEmpty()]
    [string]$param2,

    [parameter( Mandatory=$false, ParameterSetName="ParameterSet3",
                DontShow, HelpMessage="dummy parameter")]
    [string]$AnythingFake='?'

)
process {

    Write-Host $PsCmdlet.ParameterSetName -ForegroundColor Yellow
    # The following `switch` statement calls a different method 
    #       based on which parameter set the user has specified.
    switch ($PsCmdlet.ParameterSetName) 
    { 
        "ParameterSet1" { "$param1/*/*";        break} 
        "ParameterSet2" { "$param1/$param2/*";  break}
        "ParameterSet3" { "*/*/$AnythingFake";  break}
        Default         { throw "impossible";   break}
    }
}}

可能的用法示例

# Prepare parameters for pipeline
$par1 =  [PSCustomObject]@{param1='p1p'}
$par12 = [PSCustomObject]@{param1='P1P';param2='p2p'}

# Possible valid call                 # ParameterSetName
cmdletName -param1 "p1"               # ParameterSet1
cmdletName -param1 "P1" -param2 "p2"  # ParameterSet2
cmdletName                            # ParameterSet3
$par1 | cmdletName                    # ParameterSet1
$par12 | cmdletName                   # ParameterSet2

结果:

ParameterSet1
p1/*/*
ParameterSet2
P1/p2/*
ParameterSet3
*/*/?
ParameterSet1
p1p/*/*
ParameterSet2
P1P/p2p/*