-
Notifications
You must be signed in to change notification settings - Fork 5
/
DynamicParameters.ps1
27 lines (27 loc) · 1.47 KB
/
DynamicParameters.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
function Test-DynamicValidateSet { #Original: https://martin77s.wordpress.com/2014/06/09/dynamic-validateset-in-a-dynamic-parameter/
[CmdletBinding()]
Param(# Any other parameters can go here
)
DynamicParam {
$ParameterName = 'Path'
$arrSet = Get-ChildItem -Path . -Directory | Select-Object -ExpandProperty FullName
$RuntimeParameterDictionary = New-Object Management.Automation.RuntimeDefinedParameterDictionary
$RuntimeParameterDictionary.Add($ParameterName, (New-Object Management.Automation.RuntimeDefinedParameter($ParameterName, [string], @( #attributes
(New-Object Management.Automation.ParameterAttribute -Property @{Mandatory = $true; Position = 1}),
(New-Object Management.Automation.ValidateSetAttribute($arrSet))
))))
<# doesn't work
return ( New-Object Management.Automation.RuntimeDefinedParameterDictionary @{"$ParameterName" = (New-Object Management.Automation.RuntimeDefinedParameter($ParameterName, [string], @( #attributes
(New-Object Management.Automation.ParameterAttribute -Property @{Mandatory = $true; Position = 1}),
(New-Object Management.Automation.ValidateSetAttribute($arrSet))
)))} )
#>
return $RuntimeParameterDictionary
}
begin {
$Path = $PsBoundParameters[$ParameterName] # Bind the parameter to a friendly variable
}
process {# Your code goes here
dir -Path $Path
}
}