File size: 6,702 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

using namespace System.Management.Automation

#region "Replace existing function"

function Invoke-AzureFunction
{
    [Experimental("ExpTest.FeatureOne", [ExperimentAction]::Hide)]
    param(
        [string] $Token,
        [string] $Command
    )

    "Invoke-AzureFunction Version ONE"
}

function Invoke-AzureFunctionV2
{
    [Experimental("ExpTest.FeatureOne", [ExperimentAction]::Show)]
    [Alias("Invoke-AzureFunction")]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [string] $Token,

        [Parameter(Mandatory)]
        [string] $Command
    )

    "Invoke-AzureFunction Version TWO"
}

#endregion

#region "Make parameter set experimental"

function Get-GreetingMessage
{
    [CmdletBinding(DefaultParameterSetName = "Default")]
    param(
        [Parameter(Mandatory)]
        [string] $Name,

        ## If only one parameter attribute is declared for a parameter, then the parameter is
        ## hidden when the parameter attribute needs to be hide.
        [Parameter("ExpTest.FeatureOne", [ExperimentAction]::Show, ParameterSetName = "SwitchOneSet")]
        [switch] $SwitchOne,

        [Parameter("ExpTest.FeatureOne", [ExperimentAction]::Show, ParameterSetName = "SwitchTwoSet")]
        [switch] $SwitchTwo
    )

    $message = "Hello World $Name."

    if ([ExperimentalFeature]::IsEnabled("ExpTest.FeatureOne"))
    {
        if ($SwitchOne) { $message += "-SwitchOne is on." }
        if ($SwitchTwo) { $message += "-SwitchTwo is on." }
    }

    Write-Output $message
}

function Invoke-MyCommand
{
    param(
        [Parameter(Mandatory, ParameterSetName = "ComputerSet")]
        [string] $UserName,
        [Parameter(Mandatory, ParameterSetName = "ComputerSet")]
        [string] $ComputerName,

        [Parameter(Mandatory, ParameterSetName = "VMSet")]
        [string] $VMName,

        ## Enable web socket only if the feature is turned on.
        [Parameter("ExpTest.FeatureOne", [ExperimentAction]::Show, Mandatory, ParameterSetName = "WebSocketSet")]
        [string] $Token,
        [Parameter("ExpTest.FeatureOne", [ExperimentAction]::Show, Mandatory, ParameterSetName = "WebSocketSet")]
        [string] $WebSocketUrl,

        ## Add -ConfigurationName to parameter set "WebSocketSet" only if the feature is turned on.
        [Parameter(ParameterSetName = "ComputerSet")]
        [Parameter("ExpTest.FeatureOne", [ExperimentAction]::Show, ParameterSetName = "WebSocketSet")]
        [string] $ConfigurationName,

        ## Add -Port to parameter set "WebSocketSet" only if the feature is turned on.
        [Parameter(ParameterSetName = "VMSet")]
        [Parameter("ExpTest.FeatureOne", [ExperimentAction]::Show, ParameterSetName = "WebSocketSet")]
        [int] $Port,

        [int] $ThrottleLimit,
        [string] $Command
    )

    switch ($PSCmdlet.ParameterSetName)
    {
        "ComputerSet"  { "Invoke-MyCommand with ComputerSet" }
        "VMSet"        { "Invoke-MyCommand with VMSet" }
        "WebSocketSet" { "Invoke-MyCommand with WebSocketSet" }
    }
}

function Test-MyRemoting
{
    param(
        ## Replace one parameter with another one when the feature is turned on.
        [Parameter("ExpTest.FeatureOne", [ExperimentAction]::Hide)]
        [string] $SessionName,

        [Parameter("ExpTest.FeatureOne", [ExperimentAction]::Show)]
        [string] $ComputerName
    )
}

#endregion

#region "Use 'Experimental' attribute on parameters"

function Save-MyFile
{
    param(
        [Parameter(ParameterSetName = "UrlSet")]
        [switch] $ByUrl,

        [Parameter(ParameterSetName = "RadioSet")]
        [switch] $ByRadio,

        [string] $FileName,

        [Experimental("ExpTest.FeatureOne", [ExperimentAction]::Show)]
        [string] $Destination,

        [Experimental("ExpTest.FeatureOne", [ExperimentAction]::Hide)]
        [Parameter(ParameterSetName = "UrlSet")]
        [Parameter(ParameterSetName = "RadioSet")]
        [string] $Configuration
    )
}

#endregion

#region "Dynamic parameters"

function Test-MyDynamicParamOne
{
    [CmdletBinding()]
    param(
        [string] $Name
    )

    ## Use the parameter attribute to hide or show a dynamic parameter.
    DynamicParam {
        if ($Name -eq "Joe") {
            $runtimeParams = [RuntimeDefinedParameterDictionary]::new()

            $configFileAttributes = [System.Collections.ObjectModel.Collection[Attribute]]::new()
            $configFileAttributes.Add([Parameter]::new("ExpTest.FeatureOne", [ExperimentAction]::Show))
            $configFileAttributes.Add([ValidateNotNullOrEmpty]::new())
            $configFileParam = [RuntimeDefinedParameter]::new("ConfigFile", [string], $configFileAttributes)

            $configNameAttributes = [System.Collections.ObjectModel.Collection[Attribute]]::new()
            $configNameAttributes.Add([Parameter]::new("ExpTest.FeatureOne", [ExperimentAction]::Hide))
            $configNameAttributes.Add([ValidateNotNullOrEmpty]::new())
            $ConfigNameParam = [RuntimeDefinedParameter]::new("ConfigName", [string], $configNameAttributes)

            $runtimeParams.Add("ConfigFile", $configFileParam)
            $runtimeParams.Add("ConfigName", $ConfigNameParam)
            return $runtimeParams
        }
    }
}

function Test-MyDynamicParamTwo
{
    [CmdletBinding()]
    param(
        [string] $Name
    )

    ## Use the experimental attribute to hide or show a dynamic parameter.
    DynamicParam {
        if ($Name -eq "Joe") {
            $runtimeParams = [RuntimeDefinedParameterDictionary]::new()

            $configFileAttributes = [System.Collections.ObjectModel.Collection[Attribute]]::new()
            $configFileAttributes.Add([Experimental]::new("ExpTest.FeatureOne", [ExperimentAction]::Show))
            $configFileAttributes.Add([Parameter]::new())
            $configFileAttributes.Add([ValidateNotNullOrEmpty]::new())
            $configFileParam = [RuntimeDefinedParameter]::new("ConfigFile", [string], $configFileAttributes)

            $configNameAttributes = [System.Collections.ObjectModel.Collection[Attribute]]::new()
            $configNameAttributes.Add([Experimental]::new("ExpTest.FeatureOne", [ExperimentAction]::Hide))
            $configNameAttributes.Add([Parameter]::new())
            $configNameAttributes.Add([ValidateNotNullOrEmpty]::new())
            $ConfigNameParam = [RuntimeDefinedParameter]::new("ConfigName", [string], $configNameAttributes)

            $runtimeParams.Add("ConfigFile", $configFileParam)
            $runtimeParams.Add("ConfigName", $ConfigNameParam)
            return $runtimeParams
        }
    }
}

#endregion