-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNew-DynamicParameter.ps1
271 lines (231 loc) · 11 KB
/
New-DynamicParameter.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
Function New-DynamicParameter
{
<#
.SYNOPSIS
Helper function to simplify creating dynamic parameters
.DESCRIPTION
Helper function to simplify creating dynamic parameters
Example use cases:
Include parameters only if your environment dictates it
Include parameters depending on the value of a user-specified parameter
Provide tab completion and intellisense for parameters, depending on the environment
Please keep in mind that all dynamic parameters you create will not have corresponding variables created.
One of the examples illustrates a generic method for populating appropriate variables from dynamic parameters
Alternatively, manually reference $PSBoundParameters for the dynamic parameter value
.NOTES
Credit to http://jrich523.wordpress.com/2013/05/30/powershell-simple-way-to-add-dynamic-parameters-to-advanced-function/
https://raw.githubusercontent.com/RamblingCookieMonster/PowerShell/master/New-DynamicParam.ps1
MIT License
Copyright (c) 2016 Warren Frame
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
.PARAMETER Name
Name of the dynamic parameter
.PARAMETER Type
Type for the dynamic parameter. Default is string
.PARAMETER Alias
If specified, one or more aliases to assign to the dynamic parameter
.PARAMETER ValidateSet
If specified, set the ValidateSet attribute of this dynamic parameter
.PARAMETER Mandatory
If specified, set the Mandatory attribute for this dynamic parameter
.PARAMETER ParameterSetName
If specified, set the ParameterSet attribute for this dynamic parameter
.PARAMETER Position
If specified, set the Position attribute for this dynamic parameter
.PARAMETER ValueFromPipelineByPropertyName
If specified, set the ValueFromPipelineByPropertyName attribute for this dynamic parameter
.PARAMETER HelpMessage
If specified, set the HelpMessage for this dynamic parameter
.PARAMETER DPDictionary
If specified, add resulting RuntimeDefinedParameter to an existing RuntimeDefinedParameterDictionary (appropriate for multiple dynamic parameters)
If not specified, create and return a RuntimeDefinedParameterDictionary (appropriate for a single dynamic parameter)
See final example for illustration
.EXAMPLE
function Show-Free
{
[CmdletBinding()]
Param()
DynamicParam {
$options = @( gwmi win32_volume | %{$_.driveletter} | sort )
New-DynamicParam -Name Drive -ValidateSet $options -Position 0 -Mandatory
}
begin{
#have to manually populate
$drive = $PSBoundParameters.drive
}
process{
$vol = gwmi win32_volume -Filter "driveletter='$drive'"
"{0:N2}% free on {1}" -f ($vol.Capacity / $vol.FreeSpace),$drive
}
} #Show-Free
Show-Free -Drive <tab>
# This example illustrates the use of New-DynamicParam to create a single dynamic parameter
# The Drive parameter ValidateSet populates with all available volumes on the computer for handy tab completion / intellisense
.EXAMPLE
# I found many cases where I needed to add more than one dynamic parameter
# The DPDictionary parameter lets you specify an existing dictionary
# The block of code in the Begin block loops through bound parameters and defines variables if they don't exist
Function Test-DynPar{
[cmdletbinding()]
param(
[string[]]$x = $Null
)
DynamicParam
{
#Create the RuntimeDefinedParameterDictionary
$Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
New-DynamicParam -Name AlwaysParam -ValidateSet @( gwmi win32_volume | %{$_.driveletter} | sort ) -DPDictionary $Dictionary
#Add dynamic parameters to $dictionary
if($x -eq 1)
{
New-DynamicParam -Name X1Param1 -ValidateSet 1,2 -mandatory -DPDictionary $Dictionary
New-DynamicParam -Name X1Param2 -DPDictionary $Dictionary
New-DynamicParam -Name X3Param3 -DPDictionary $Dictionary -Type DateTime
}
else
{
New-DynamicParam -Name OtherParam1 -Mandatory -DPDictionary $Dictionary
New-DynamicParam -Name OtherParam2 -DPDictionary $Dictionary
New-DynamicParam -Name OtherParam3 -DPDictionary $Dictionary -Type DateTime
}
#return RuntimeDefinedParameterDictionary
$Dictionary
}
Begin
{
#This standard block of code loops through bound parameters...
#If no corresponding variable exists, one is created
#Get common parameters, pick out bound parameters not in that set
Function _temp { [cmdletbinding()] param() }
$BoundKeys = $PSBoundParameters.keys | Where-Object { (get-command _temp | select -ExpandProperty parameters).Keys -notcontains $_}
foreach($param in $BoundKeys)
{
if (-not ( Get-Variable -name $param -scope 0 -ErrorAction SilentlyContinue ) )
{
New-Variable -Name $Param -Value $PSBoundParameters.$param
Write-Verbose "Adding variable for dynamic parameter '$param' with value '$($PSBoundParameters.$param)'"
}
}
#Appropriate variables should now be defined and accessible
Get-Variable -scope 0
}
}
# This example illustrates the creation of many dynamic parameters using New-DynamicParam
# You must create a RuntimeDefinedParameterDictionary object ($dictionary here)
# To each New-DynamicParam call, add the -DPDictionary parameter pointing to this RuntimeDefinedParameterDictionary
# At the end of the DynamicParam block, return the RuntimeDefinedParameterDictionary
# Initialize all bound parameters using the provided block or similar code
.FUNCTIONALITY
PowerShell Language
#>
[cmdletbinding()]
param
(
[parameter(Mandatory)]
[string]$Name
,
[parameter()]
[System.Type]$Type = [string]
,
[parameter()]
[string[]]$Alias = @()
,
[parameter()]
[string[]]$ValidateSet
,
[parameter()]
[bool]$ValidateNotNullOrEmpty
,
[parameter()]
[bool]$Mandatory = $true
,
[parameter()]
[string]$ParameterSetName = "__AllParameterSets"
,
[parameter()]
[int]$Position
,
[parameter()]
[bool]$ValueFromPipelineByPropertyName = $false
,
[parameter()]
[bool]$ValueFromPipeline = $false
,
[parameter()]
[string]$HelpMessage
,
[parameter()]
$DefaultValue
,
[parameter()]
$DPDictionary
)
$ParamAttr = New-Object System.Management.Automation.ParameterAttribute
$ParamAttr.ParameterSetName = $ParameterSetName
$ParamAttr.Mandatory = $Mandatory
if ($PSBoundParameters.ContainsKey('Position'))
{
$ParamAttr.Position = $Position
}
$ParamAttr.ValueFromPipelineByPropertyName = $ValueFromPipelineByPropertyName
$ParamAttr.ValueFromPipeline = $ValueFromPipeline
if ($PSboundParameters.ContainsKey('HelpMessage'))
{
$ParamAttr.HelpMessage = $HelpMessage
}
$AttributeCollection = New-Object 'Collections.ObjectModel.Collection[System.Attribute]'
$AttributeCollection.Add($ParamAttr)
if ($PSBoundParameters.ContainsKey('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)
#Set the default value #added by MC
if (
#$PSBoundParameters.ContainsKey($DefaultValue)
$null -ne $DefaultValue
)
{
Write-Verbose -Message "adding Default Value to Parameter $($Parameter.Name)"
$Parameter.Value = $DefaultValue
}
#Add the dynamic parameter to an existing dynamic parameter dictionary, or create the dictionary and add it
if (-not $null -eq $DPDictionary)
{
Write-Verbose -Message "Using Existing DPDictionary"
if (-not $DPDictionary.ContainsKey($Name))
{
$DPDictionary.Add($Name, $Parameter)
}
$DPDictionary
}
else
{
#Write-Verbose -Message "Creating New DPDictionary"
$Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$Dictionary.Add($Name, $Parameter)
$Dictionary
}
}