Skip to content

Instantly share code, notes, and snippets.

@mabster
Created October 12, 2020 02:42
Show Gist options
  • Save mabster/494109ba67c177806db7fb364b58f446 to your computer and use it in GitHub Desktop.
Save mabster/494109ba67c177806db7fb364b58f446 to your computer and use it in GitHub Desktop.
[cmdletbinding()]
param (
[parameter(parametersetname='test1')]
[switch]$p1,
[parameter(parametersetname='test2')]
[switch]$p2
)
DynamicParam
{
switch ($pscmdlet.parametersetname) {
test1
{
$attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributes = New-Object -Type System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = "test1"
$attributes.Mandatory = $false
$attributeCollection.Add($attributes)
$attributes = New-Object -Type System.Management.Automation.ValidateRangeAttribute(1, 3)
$attributeCollection.Add($attributes)
$dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Count", [Int32], $attributeCollection)
$paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add("Count", $dynParam1)
return $paramDictionary
}
test2
{
$attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributes = New-Object -Type System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = "test2"
$attributes.Mandatory = $false
$attributeCollection.Add($attributes)
$attributes = New-Object -Type System.Management.Automation.ValidateRangeAttribute(1, 10)
$attributeCollection.Add($attributes)
$dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Count", [Int32], $attributeCollection)
$paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add("Count", $dynParam1)
return $paramDictionary
}
}
}
process {
write-output $psboundparameters.Count
}
@thedavecarroll
Copy link

Thanks guys.

Definitely something to look into over the next few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment