validateset的动态参数和基于动态validateset的值的条件参数

时间:2019-07-02 20:58:55

标签: powershell powershell-v5.1

是否可以使用PowerShell的动态参数功能基于所述参数的值创建validateset参数和条件参数?

在脚本中,我使用动态参数来生成一个参数,该参数是计算出的validateset。我想向脚本添加一个条件参数,该条件参数仅基于validateset中的值才需要。

这是我要完成的示例代码。在此示例代码中,从未创建条件参数,因为从未在$PSBoundParameter.Product块中设置DynamicParam。使用PowerShell可以实现这种功能吗?

function Get-Order {
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory=$true,
            Position=1,
            HelpMessage="How many cups would you like to purchase?"
        )]
        [int]$cups
    )

    DynamicParam {
        Write-Warning "This gets printed"
        ## Product parameter with dynamic ValidateSet
        $productAttribute = new-object System.Management.Automation.ParameterAttribute
        $productAttribute.Position = 2
        $productAttribute.Mandatory = $false
        $productAttribute.HelpMessage = "What would you like to purchase?"

        #create an attributecollection object for the attribute we just created.
        $attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]

        #add our custom attribute
        $attributeCollection.Add($productAttribute)

        #add our paramater specifying the attribute collection
        $arrSet = @("Lemonade","Water","Tea","Coffee","Hard Lemonade") # In my actual script this is generated not hard coded.
        $ValidateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
        $AttributeCollection.Add($ValidateSetAttribute)

        $productParam = new-object -Type System.Management.Automation.RuntimeDefinedParameter("Product", [string], $attributeCollection)

        #expose the name of our parameter
        $paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
        $paramDictionary.Add('Product', $productParam)

        if ($PSBoundParameters.Product -eq "Hard Lemonade") {
              Write-Warning "Never printed"
              #create a new ParameterAttribute Object
              $ageAttribute = New-Object System.Management.Automation.ParameterAttribute
              $ageAttribute.Position = 3
              $ageAttribute.Mandatory = $true
              $ageAttribute.HelpMessage = "This product is only available for customers 21 years of age and older. Please enter your age:"

              #create an attributecollection object for the attribute we just created.
              $attributeCollection = new-object System.Collections.ObjectModel.Collection[System.Attribute]

              #add our custom attribute
              $attributeCollection.Add($ageAttribute)

              #add our paramater specifying the attribute collection
              $ageParam = New-Object System.Management.Automation.RuntimeDefinedParameter('age', [Int16], $attributeCollection)

              #expose the name of our parameter
              $paramDictionary.Add('age', $ageParam)
        }

        return $paramDictionary
    }

    Process {
        $order = @()
        for ($cup = 1; $cup -le $cups; $cup++) {
            $order += "$($cup): A cup of $($product)"
        }
        $order
    }
}

注意:此代码基于PowerShellMagazine的示例代码。

0 个答案:

没有答案