-
Notifications
You must be signed in to change notification settings - Fork 8
/
Utility.ps1
42 lines (37 loc) · 1.24 KB
/
Utility.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
function New-DynamicParam {
param(
[string]
$Name,
[string[]]
$Options=$null,
[switch]
$Mandatory,
[string]
$SetName="__AllParameterSets",
[int]
$Position,
[switch]
$ValueFromPipelineByPropertyName,
[string]
$HelpMessage
)
#param attributes
$ParamAttr = New-Object System.Management.Automation.ParameterAttribute
$ParamAttr.ParameterSetName = $SetName
if($Mandatory){ $ParamAttr.Mandatory = $True }
if($Position -ne $null){$ParamAttr.Position=$Position}
if($ValueFromPipelineByPropertyName){$ParamAttr.ValueFromPipelineByPropertyName = $True}
if($HelpMessage){$ParamAttr.HelpMessage = $HelpMessage}
$AttributeCollection = New-Object 'Collections.ObjectModel.Collection[System.Attribute]'
$AttributeCollection.Add($ParamAttr)
if($Options)
{
$ParamOptions = New-Object System.Management.Automation.ValidateSetAttribute -ArgumentList $options
$AttributeCollection.Add($ParamOptions)
}
$Parameter = New-Object -TypeName System.Management.Automation.RuntimeDefinedParameter `
-ArgumentList @($Name, [string], $AttributeCollection)
$Dictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$Dictionary.Add($Name, $Parameter)
$Dictionary
}