File size: 7,127 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Management.Automation;

namespace ExperimentalFeatureTest
{
    #region "Replace existing cmdlet"

    [Experimental("ExpTest.FeatureOne", ExperimentAction.Hide)]
    [Cmdlet("Invoke", "AzureFunctionCSharp")]
    public class InvokeAzureFunctionCommand : PSCmdlet
    {
        [Parameter]
        public string Token { get; set; }

        [Parameter]
        public string Command { get; set; }

        protected override void EndProcessing()
        {
            WriteObject("Invoke-AzureFunction Version ONE");
        }
    }

    [Experimental("ExpTest.FeatureOne", ExperimentAction.Show)]
    [Cmdlet("Invoke", "AzureFunctionCSharp")]
    public class InvokeAzureFunctionCommandV2 : PSCmdlet
    {
        [Parameter(Mandatory = true)]
        public string Token { get; set; }

        [Parameter(Mandatory = true)]
        public string Command { get; set; }

        protected override void EndProcessing()
        {
            WriteObject("Invoke-AzureFunction Version TWO");
        }
    }

    #endregion

    #region "Make parameter set experimental"

    [Cmdlet("Get", "GreetingMessageCSharp", DefaultParameterSetName = "Default")]
    public class GetGreetingMessageCommand : PSCmdlet
    {
        [Parameter(Mandatory = true)]
        public string Name { get; set; }

        [Parameter("ExpTest.FeatureOne", ExperimentAction.Show, ParameterSetName = "SwitchOneSet")]
        public SwitchParameter SwitchOne { get; set; }

        [Parameter("ExpTest.FeatureOne", ExperimentAction.Show, ParameterSetName = "SwitchTwoSet")]
        public SwitchParameter SwitchTwo { get; set; }

        protected override void EndProcessing()
        {
            string message = $"Hello World {Name}.";
            if (ExperimentalFeature.IsEnabled("ExpTest.FeatureOne"))
            {
                if (SwitchOne.IsPresent)
                {
                    message += "-SwitchOne is on.";
                }

                if (SwitchTwo.IsPresent)
                {
                    message += "-SwitchTwo is on.";
                }
            }

            WriteObject(message);
        }
    }

    [Cmdlet("Invoke", "MyCommandCSharp")]
    public class InvokeMyCommandCommand : PSCmdlet
    {
        [Parameter(Mandatory = true, ParameterSetName = "ComputerSet")]
        public string UserName { get; set; }

        [Parameter(Mandatory = true, ParameterSetName = "ComputerSet")]
        public string ComputerName { get; set; }

        [Parameter(Mandatory = true, ParameterSetName = "VMSet")]
        public string VMName { get; set; }

        // Enable web socket only if the feature is turned on.
        [Parameter("ExpTest.FeatureOne", ExperimentAction.Show, Mandatory = true, ParameterSetName = "WebSocketSet")]
        public string Token { get; set; }

        [Parameter("ExpTest.FeatureOne", ExperimentAction.Show, Mandatory = true, ParameterSetName = "WebSocketSet")]
        public string WebSocketUrl { get; set; }

        // Add -ConfigurationName to parameter set "WebSocketSet" only if the feature is turned on.
        [Parameter(ParameterSetName = "ComputerSet")]
        [Parameter("ExpTest.FeatureOne", ExperimentAction.Show, ParameterSetName = "WebSocketSet")]
        public string ConfigurationName { get; set; }

        // Add -Port to parameter set "WebSocketSet" only if the feature is turned on.
        [Parameter(ParameterSetName = "VMSet")]
        [Parameter("ExpTest.FeatureOne", ExperimentAction.Show, ParameterSetName = "WebSocketSet")]
        public int Port { get; set; }

        [Parameter]
        public int ThrottleLimit { get; set; }

        [Parameter]
        public string Command { get; set; }

        protected override void EndProcessing()
        {
            switch (this.ParameterSetName)
            {
                case "ComputerSet": WriteObject("Invoke-MyCommand with ComputerSet"); break;
                case "VMSet": WriteObject("Invoke-MyCommand with VMSet"); break;
                case "WebSocketSet": WriteObject("Invoke-MyCommand with WebSocketSet"); break;
                default: break;
            }
        }
    }

    [Cmdlet("Test", "MyRemotingCSharp")]
    public class TestMyRemotingCommand : PSCmdlet
    {
        // Replace one parameter with another one when the feature is turned on.
        [Parameter("ExpTest.FeatureOne", ExperimentAction.Hide)]
        public string SessionName { get; set; }

        [Parameter("ExpTest.FeatureOne", ExperimentAction.Show)]
        public string ComputerName { get; set; }

        protected override void EndProcessing() { }
    }

    #endregion

    #region "Use 'Experimental' attribute on parameters"

    [Cmdlet("Save", "MyFileCSharp")]
    public class SaveMyFileCommand : PSCmdlet
    {
        [Parameter(ParameterSetName = "UrlSet")]
        public SwitchParameter ByUrl { get; set; }

        [Parameter(ParameterSetName = "RadioSet")]
        public SwitchParameter ByRadio { get; set; }

        [Parameter]
        public string FileName { get; set; }

        [Experimental("ExpTest.FeatureOne", ExperimentAction.Show)]
        [Parameter]
        public string Destination { get; set; }

        [Experimental("ExpTest.FeatureOne", ExperimentAction.Hide)]
        [Parameter(ParameterSetName = "UrlSet")]
        [Parameter(ParameterSetName = "RadioSet")]
        public string Configuration { get; set; }

        protected override void EndProcessing() { }
    }

    #endregion

    #region "Dynamic parameters"

    public class DynamicParamOne
    {
        [Parameter("ExpTest.FeatureOne", ExperimentAction.Show)]
        [ValidateNotNullOrEmpty]
        public string ConfigFile { get; set; }

        [Parameter("ExpTest.FeatureOne", ExperimentAction.Hide)]
        [ValidateNotNullOrEmpty]
        public string ConfigName { get; set; }
    }

    [Cmdlet("Test", "MyDynamicParamOneCSharp")]
    public class TestMyDynamicParamOneCommand : PSCmdlet, IDynamicParameters
    {
        [Parameter(Position = 0)]
        public string Name { get; set; }

        public object GetDynamicParameters()
        {
            return Name == "Joe" ? new DynamicParamOne() : null;
        }

        protected override void EndProcessing() { }
    }

    public class DynamicParamTwo
    {
        [Experimental("ExpTest.FeatureOne", ExperimentAction.Show)]
        [Parameter]
        [ValidateNotNullOrEmpty]
        public string ConfigFile { get; set; }

        [Experimental("ExpTest.FeatureOne", ExperimentAction.Hide)]
        [Parameter]
        [ValidateNotNullOrEmpty]
        public string ConfigName { get; set; }
    }

    [Cmdlet("Test", "MyDynamicParamTwoCSharp")]
    public class TestMyDynamicParamTwoCommand : PSCmdlet, IDynamicParameters
    {
        [Parameter(Position = 0)]
        public string Name { get; set; }

        public object GetDynamicParameters()
        {
            return Name == "Joe" ? new DynamicParamTwo() : null;
        }

        protected override void EndProcessing() { }
    }

    #endregion
}