|
21 | 21 | Added logic to add RuntimeDefinedParameter to existing DPDictionary
|
22 | 22 | Added a little comment based help
|
23 | 23 |
|
| 24 | + Credit to BM for alias and type parameters and their handling |
| 25 | +
|
24 | 26 | .PARAMETER Name
|
25 | 27 | Name of the dynamic parameter
|
26 | 28 |
|
| 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 | +
|
27 | 35 | .PARAMETER ValidateSet
|
28 | 36 | If specified, set the ValidateSet attribute of this dynamic parameter
|
29 | 37 |
|
|
46 | 54 | If specified, add resulting RuntimeDefinedParameter to an existing RuntimeDefinedParameterDictionary (appropriate for multiple dynamic parameters)
|
47 | 55 | If not specified, create and return a RuntimeDefinedParameterDictionary (appropriate for a single dynamic parameter)
|
48 | 56 |
|
| 57 | + See final example for illustration |
| 58 | +
|
49 | 59 | .EXAMPLE
|
50 | 60 |
|
51 | 61 | function Show-Free
|
|
73 | 83 |
|
74 | 84 | .EXAMPLE
|
75 | 85 |
|
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 |
77 | 87 | # The DPDictionary parameter lets you specify an existing dictionary
|
78 | 88 | # The block of code in the Begin block loops through bound parameters and defines variables if they don't exist
|
79 | 89 |
|
|
87 | 97 | #Create the RuntimeDefinedParameterDictionary
|
88 | 98 | $Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
|
89 | 99 |
|
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 |
91 | 101 |
|
92 | 102 | #Add dynamic parameters to $dictionary
|
93 | 103 | if($x -eq 1)
|
94 | 104 | {
|
95 |
| - New-DynamicParam -Name X1Param1 -Options 1,2 -mandatory -DPDictionary $Dictionary |
| 105 | + New-DynamicParam -Name X1Param1 -ValidateSet 1,2 -mandatory -DPDictionary $Dictionary |
96 | 106 | New-DynamicParam -Name X1Param2 -DPDictionary $Dictionary
|
97 |
| - New-DynamicParam -Name X3Param3 -DPDictionary $Dictionary |
| 107 | + New-DynamicParam -Name X3Param3 -DPDictionary $Dictionary -Type DateTime |
98 | 108 | }
|
99 | 109 | else
|
100 | 110 | {
|
101 |
| - New-DynamicParam -Name OtherParam1 -mandatory -DPDictionary $Dictionary |
| 111 | + New-DynamicParam -Name OtherParam1 -Mandatory -DPDictionary $Dictionary |
102 | 112 | New-DynamicParam -Name OtherParam2 -DPDictionary $Dictionary
|
103 |
| - New-DynamicParam -Name OtherParam3 -DPDictionary $Dictionary |
| 113 | + New-DynamicParam -Name OtherParam3 -DPDictionary $Dictionary -Type DateTime |
104 | 114 | }
|
105 | 115 |
|
106 | 116 | #return RuntimeDefinedParameterDictionary
|
|
110 | 120 | {
|
111 | 121 | #This standard block of code loops through bound parameters...
|
112 | 122 | #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) |
114 | 127 | {
|
115 | 128 | if (-not ( Get-Variable -name $param -scope 0 -ErrorAction SilentlyContinue ) )
|
116 | 129 | {
|
|
129 | 142 | # To each New-DynamicParam call, add the -DPDictionary parameter pointing to this RuntimeDefinedParameterDictionary
|
130 | 143 | # At the end of the DynamicParam block, return the RuntimeDefinedParameterDictionary
|
131 | 144 | # Initialize all bound parameters using the provided block or similar code
|
| 145 | +
|
| 146 | + .FUNCTIONALITY |
| 147 | + PowerShell Language |
| 148 | +
|
132 | 149 | #>
|
133 | 150 | param(
|
134 | 151 |
|
135 | 152 | [string]
|
136 | 153 | $Name,
|
137 | 154 |
|
| 155 | + [System.Type] |
| 156 | + $Type = [string], |
| 157 | + |
| 158 | + [string[]] |
| 159 | + $Alias = @(), |
| 160 | + |
138 | 161 | [string[]]
|
139 | 162 | $ValidateSet,
|
140 | 163 |
|
@@ -193,9 +216,15 @@ param(
|
193 | 216 | $AttributeCollection.Add($ParamOptions)
|
194 | 217 | }
|
195 | 218 |
|
| 219 | + #Aliases if specified |
| 220 | + if($Alias) { |
| 221 | + $ParamAlias = New-Object System.Management.Automation.AliasAttribute -ArgumentList $Alias |
| 222 | + $AttributeCollection.Add($ParamAlias) |
| 223 | + } |
| 224 | + |
196 | 225 |
|
197 | 226 | #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) |
199 | 228 |
|
200 | 229 | #Add the dynamic parameter to an existing dynamic parameter dictionary, or create the dictionary and add it
|
201 | 230 | if($DPDictionary)
|
|
0 commit comments