-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeDynamicParam.ps1
153 lines (127 loc) · 3.76 KB
/
makeDynamicParam.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
<#
.SYNOPSIS
Helper for making dynamic parameters with ValidateSets in PowerShell
.PARAMETER ParameterName
Name of dynamic parameter
.PARAMETER MakeList
A scriptblock to create the ValidateSet list
.PARAMETER Alias
Alias for parameter
.PARAMETER ParameterSetName
Parameter set name
.PARAMETER Mandatory
If the parameter is mandatory
.PARAMETER ValueFromPipeline
If the parameter is from the pipeline
.PARAMETER Position
Position to set
.PARAMETER Help
Optional help
.PARAMETER DebugFile
File for outputing debug information, for debugging dyn parameters
.EXAMPLE
function simpleDynamicParam {
[CmdletBinding()]
param()
DynamicParam
{
makeDynamicParam "dyn" -MakeList {
return 'cow','pig','horse'
}
}
process
{
Set-StrictMode -Version Latest
$dyn = $psboundparameters["dyn"]
Write-Verbose "params are $($psboundparameters | out-string)"
$dyn
}
}
.EXAMPLE
function conditionalDynamicParam {
[CmdletBinding()]
param(
[switch] $Colors
)
DynamicParam
{
makeDynamicParam "dyn" -MakeList {
if ( (Test-Path variable:colors) -and $colors )
{
'red','light blue','green'
}
}
}
#>
function makeDynamicParam
{
[CmdletBinding()]
param(
[Parameter(Mandatory,ParameterSetName="ValidateScriptBlock",Position=1)]
[Parameter(Mandatory,ParameterSetName="ValidateSet",Position=1)]
[string] $ParameterName,
[Parameter(Mandatory,ParameterSetName="ValidateScriptBlock")]
[scriptblock] $ValidateSetScript,
[Parameter(Mandatory,ParameterSetName="ValidateSet")]
[string[]] $ValidateSet,
[string] $Alias,
[string] $ParameterSetName,
[switch] $Mandatory,
[switch] $ValueFromPipeline,
[switch] $ValueFromPipelineByPropertyName,
[int] $Position = 0,
[string] $Help,
[string] $DebugFile,
[Parameter(ValueFromPipeline)]
[System.Management.Automation.RuntimeDefinedParameterDictionary] $ParamDictionary
)
Set-StrictMode -Version Latest
function logit($msg) {
if ( $DebugFile ) { "$(Get-Date -form 's') $msg" | out-file $DebugFile -Append -Encoding utf8 }
}
logit "makeDynamicParam for $ParameterName"
# create a dictionary to return,
if ( !$ParamDictionary )
{
$ParamDictionary = new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
}
# create a new [string] dyn parameter with a collection of attributes
$attributeCollection = new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$dynParam = new-object -Type System.Management.Automation.RuntimeDefinedParameter($ParameterName, [String], $attributeCollection)
# create a new atrbute for all parameter sets
$attributes = new-object System.Management.Automation.ParameterAttribute
if ( $ParameterSetName )
{
$attributes.ParameterSetName = $ParameterSetName
}
if ( $Help )
{
$attributes.HelpMessage = $Help
}
$attributes.Mandatory = [bool]$Mandatory
$attributes.ValueFromPipeline = [bool]$ValueFromPipeline
$attributes.ValueFromPipelineByPropertyName = [bool]$ValueFromPipelineByPropertyName
$attributes.Position = $Position
logit "Attributes are $(ConvertTo-Json ($attributes | Select-Object * -ExcludeProperty "TypeId") -Depth 1)"
if ( $ValidateSetScript )
{
try
{
logit "About to invoke"
$ValidateSet = $ValidateSetScript.Invoke()
}
catch {
logit "Exception from ValidateSetScript: $_"
}
}
logit "list is now $($ValidateSet | out-string)"
if ( $ValidateSet )
{
$paramOptions = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $ValidateSet
$attributeCollection.Add($paramOptions)
}
# hook things together
$attributeCollection.Add($attributes)
$ParamDictionary.Add($ParameterName, $dynParam)
return $ParamDictionary
}