Skip to content

Commit f43718d

Browse files
New-DynamicParam - added type, alias params
1 parent 16bf9f0 commit f43718d

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed

New-DynamicParam.ps1

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@
2121
Added logic to add RuntimeDefinedParameter to existing DPDictionary
2222
Added a little comment based help
2323
24+
Credit to BM for alias and type parameters and their handling
25+
2426
.PARAMETER Name
2527
Name of the dynamic parameter
2628
29+
.PARAMETER Type
30+
Type for the dynamic parameter. Default is string
31+
32+
.PARAMETER Alias
33+
If specified, one or more aliases to assign to the dynamic parameter
34+
2735
.PARAMETER ValidateSet
2836
If specified, set the ValidateSet attribute of this dynamic parameter
2937
@@ -46,6 +54,8 @@
4654
If specified, add resulting RuntimeDefinedParameter to an existing RuntimeDefinedParameterDictionary (appropriate for multiple dynamic parameters)
4755
If not specified, create and return a RuntimeDefinedParameterDictionary (appropriate for a single dynamic parameter)
4856
57+
See final example for illustration
58+
4959
.EXAMPLE
5060
5161
function Show-Free
@@ -73,7 +83,7 @@
7383
7484
.EXAMPLE
7585
76-
# I found many cases where I needed to add many dynamic parameters
86+
# I found many cases where I needed to add more than one dynamic parameter
7787
# The DPDictionary parameter lets you specify an existing dictionary
7888
# The block of code in the Begin block loops through bound parameters and defines variables if they don't exist
7989
@@ -87,20 +97,20 @@
8797
#Create the RuntimeDefinedParameterDictionary
8898
$Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
8999
90-
New-DynamicParam -Name AlwaysParam -options @( gwmi win32_volume | %{$_.driveletter} | sort ) -DPDictionary $Dictionary
100+
New-DynamicParam -Name AlwaysParam -ValidateSet @( gwmi win32_volume | %{$_.driveletter} | sort ) -DPDictionary $Dictionary
91101
92102
#Add dynamic parameters to $dictionary
93103
if($x -eq 1)
94104
{
95-
New-DynamicParam -Name X1Param1 -Options 1,2 -mandatory -DPDictionary $Dictionary
105+
New-DynamicParam -Name X1Param1 -ValidateSet 1,2 -mandatory -DPDictionary $Dictionary
96106
New-DynamicParam -Name X1Param2 -DPDictionary $Dictionary
97-
New-DynamicParam -Name X3Param3 -DPDictionary $Dictionary
107+
New-DynamicParam -Name X3Param3 -DPDictionary $Dictionary -Type DateTime
98108
}
99109
else
100110
{
101-
New-DynamicParam -Name OtherParam1 -mandatory -DPDictionary $Dictionary
111+
New-DynamicParam -Name OtherParam1 -Mandatory -DPDictionary $Dictionary
102112
New-DynamicParam -Name OtherParam2 -DPDictionary $Dictionary
103-
New-DynamicParam -Name OtherParam3 -DPDictionary $Dictionary
113+
New-DynamicParam -Name OtherParam3 -DPDictionary $Dictionary -Type DateTime
104114
}
105115
106116
#return RuntimeDefinedParameterDictionary
@@ -110,7 +120,10 @@
110120
{
111121
#This standard block of code loops through bound parameters...
112122
#If no corresponding variable exists, one is created
113-
foreach($param in $PSBoundParameters.Keys)
123+
#Get common parameters, pick out bound parameters not in that set
124+
Function _temp { [cmdletbinding()] param() }
125+
$BoundLeys = $PSBoundParameters.keys | Where-Object { (get-command _temp | select -ExpandProperty parameters).Keys -notcontains $_}
126+
foreach($param in $BoundKeys)
114127
{
115128
if (-not ( Get-Variable -name $param -scope 0 -ErrorAction SilentlyContinue ) )
116129
{
@@ -129,12 +142,22 @@
129142
# To each New-DynamicParam call, add the -DPDictionary parameter pointing to this RuntimeDefinedParameterDictionary
130143
# At the end of the DynamicParam block, return the RuntimeDefinedParameterDictionary
131144
# Initialize all bound parameters using the provided block or similar code
145+
146+
.FUNCTIONALITY
147+
PowerShell Language
148+
132149
#>
133150
param(
134151

135152
[string]
136153
$Name,
137154

155+
[System.Type]
156+
$Type = [string],
157+
158+
[string[]]
159+
$Alias = @(),
160+
138161
[string[]]
139162
$ValidateSet,
140163

@@ -193,9 +216,15 @@ param(
193216
$AttributeCollection.Add($ParamOptions)
194217
}
195218

219+
#Aliases if specified
220+
if($Alias) {
221+
$ParamAlias = New-Object System.Management.Automation.AliasAttribute -ArgumentList $Alias
222+
$AttributeCollection.Add($ParamAlias)
223+
}
224+
196225

197226
#Create the dynamic parameter
198-
$Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList @($Name, [string], $AttributeCollection)
227+
$Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter -ArgumentList @($Name, $Type, $AttributeCollection)
199228

200229
#Add the dynamic parameter to an existing dynamic parameter dictionary, or create the dictionary and add it
201230
if($DPDictionary)

0 commit comments

Comments
 (0)