Skip to content

Allow multiple parametersetname for dynamicparam #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 34 additions & 37 deletions New-DynamicParam.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Function New-DynamicParam {
Function New-DynamicParam {
<#
.SYNOPSIS
Helper function to simplify creating dynamic parameters
Expand Down Expand Up @@ -40,6 +40,7 @@

.PARAMETER ParameterSetName
If specified, set the ParameterSet attribute for this dynamic parameter
You can provide a [string[]] array of parameter sets if you need this in multiple

.PARAMETER Position
If specified, set the Position attribute for this dynamic parameter
Expand Down Expand Up @@ -164,7 +165,7 @@ param(
[switch]
$Mandatory,

[string]
[string[]]
$ParameterSetName="__AllParameterSets",

[int]
Expand All @@ -188,41 +189,37 @@ param(
)
#Create attribute object, add attributes, add to collection
$ParamAttr = New-Object System.Management.Automation.ParameterAttribute
$ParamAttr.ParameterSetName = $ParameterSetName
if($mandatory)
{
$ParamAttr.Mandatory = $True
}
if($Position -ne $null)
{
$ParamAttr.Position=$Position
}
if($ValueFromPipelineByPropertyName)
{
$ParamAttr.ValueFromPipelineByPropertyName = $True
}
if($HelpMessage)
{
$ParamAttr.HelpMessage = $HelpMessage
}

$AttributeCollection = New-Object 'Collections.ObjectModel.Collection[System.Attribute]'
$AttributeCollection.Add($ParamAttr)

#param validation set if specified
if($ValidateSet)
{
$ParamOptions = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $ValidateSet
$AttributeCollection.Add($ParamOptions)
}

#Aliases if specified
if($Alias.count -gt 0) {
$ParamAlias = New-Object System.Management.Automation.AliasAttribute -ArgumentList $Alias
$AttributeCollection.Add($ParamAlias)
}

foreach ($Set in $ParameterSetName) {
$ParamAttr = New-Object System.Management.Automation.ParameterAttribute
$ParamAttr.ParameterSetName = $Set
if ($mandatory) {
$ParamAttr.Mandatory = $True
}
if ($null -ne $Position) {
$ParamAttr.Position = $Position
}
if ($ValueFromPipelineByPropertyName) {
$ParamAttr.ValueFromPipelineByPropertyName = $True
}
if ($HelpMessage) {
$ParamAttr.HelpMessage = $HelpMessage
}

$AttributeCollection.Add($ParamAttr)
#param validation set if specified
if ($ValidateSet) {
$ParamOptions = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $ValidateSet
$AttributeCollection.Add($ParamOptions)
}

#Aliases if specified
if ($Alias.count -gt 0) {
$ParamAlias = New-Object System.Management.Automation.AliasAttribute -ArgumentList $Alias
$AttributeCollection.Add($ParamAlias)
}
}


#Create the dynamic parameter
$Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList @($Name, $Type, $AttributeCollection)

Expand All @@ -237,4 +234,4 @@ param(
$Dictionary.Add($Name, $Parameter)
$Dictionary
}
}
}