torsdag den 7. april 2011

PSCmdlet with several ParameterSets containing same Parameter

One thing I kept running into when creating CmdLet’s was when I want to give the user the ability to pass in several parameters as both object or by ID. Troubleshooting passing in this parameters can be a bit annoying so here's a few pointers.

Imagine you have a PSCmdlet that needs a customer and a plan. lets call it New-CustomerService and the parameters Customer or CustomerID and ServicePlan or ServicePlanID this will give a total of 4 different ways of passing parameters hence 4 ParameterSetName names.

 

Private _Customer As New List(Of wsCloudAPI.extCustomer)
<Parameter(Position:=0, Mandatory:=True, ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, ParameterSetName:="set1")> _
<Parameter(Position:=0, Mandatory:=True, ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, ParameterSetName:="set2")> _
<ValidateNotNullOrEmpty()> _
Public Property Customer() As List(Of wsCloudAPI.extCustomer)
    Get
        Return _Customer
    End Get
    Set(ByVal value As List(Of wsCloudAPI.extCustomer))
        _Customer = value
        For Each o In _Customer
            _CustomerID.Add(o.CustomerID)
        Next
    End Set
End Property

Private _CustomerID As New List(Of Integer)
<Parameter(Position:=0, Mandatory:=True, ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, ParameterSetName:="set3")> _
<Parameter(Position:=0, Mandatory:=True, ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, ParameterSetName:="set4")> _
<ValidateNotNullOrEmpty()> _
Public Property CustomerID() As List(Of Integer)
    Get
        Return _CustomerID
    End Get
    Set(ByVal value As List(Of Integer))
        _CustomerID = value
    End Set
End Property

Private _ServicePlan As New List(Of wsCloudAPI.extServicePlan)
<Parameter(Position:=1, Mandatory:=True, ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, ParameterSetName:="set1")> _
<Parameter(Position:=1, Mandatory:=True, ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, ParameterSetName:="set3")> _
<ValidateNotNullOrEmpty()> _
Public Property ServicePlan() As List(Of wsCloudAPI.extServicePlan)
    Get
        Return _ServicePlan
    End Get
    Set(ByVal value As List(Of wsCloudAPI.extServicePlan))
        _ServicePlan = value
        For Each o In _ServicePlan
            _PlanID.Add(o.PlanID)
        Next
    End Set
End Property

Private _PlanID As New List(Of Integer)
<Parameter(Position:=1, Mandatory:=True, ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, ParameterSetName:="set2")> _
<Parameter(Position:=1, Mandatory:=True, ValueFromPipeline:=True, ValueFromPipelineByPropertyName:=True, ParameterSetName:="set4")> _
<ValidateNotNullOrEmpty()> _
Public Property PlanID() As List(Of Integer)
    Get
        Return _PlanID
    End Get
    Set(ByVal value As List(Of Integer))
        _PlanID = value
    End Set
End Property

Testing if this work, can then be done with

trace-command parameterbinding -pshost { New-CustomerService $customer -ServicePlan $plan }

But there is another cute way of checking the different parameter sets


(get-command New-CustomerService).ParameterSets | foreach {
$ps = $_.name
$_.Parameters | Select @{n="ParameterName";e={$_.name}},@{n="Mandatory";e={$_.IsMandatory}},@{n="Position";e={$_.Position}},@{n="HelpMessage";e={$_.HelpMessage}},@{n="ParameterSet";e={$ps}} | Format-Table
}

Ingen kommentarer:

Send en kommentar