File size: 5,712 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Management.Automation;

namespace Microsoft.PowerShell.Commands
{
    /// <summary>
    /// A cmdlet that sets the properties of the TraceSwitch instances that are instantiated in the process.
    /// </summary>
    [Cmdlet(VerbsCommon.Set, "TraceSource", DefaultParameterSetName = "optionsSet", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2097129")]
    [OutputType(typeof(PSTraceSource))]
    public class SetTraceSourceCommand : TraceListenerCommandBase
    {
        #region Parameters

        /// <summary>
        /// The TraceSource parameter determines which TraceSource categories the
        /// operation will take place on.
        /// </summary>
        [Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string[] Name
        {
            get { return base.NameInternal; }

            set { base.NameInternal = value; }
        }

        /// <summary>
        /// The flags to be set on the TraceSource.
        /// </summary>
        [Parameter(Position = 1, ValueFromPipelineByPropertyName = true, ParameterSetName = "optionsSet")]
        public PSTraceSourceOptions Option
        {
            get
            {
                return base.OptionsInternal;
            }

            set
            {
                base.OptionsInternal = value;
            }
        }

        /// <summary>
        /// The parameter which determines the options for output from the trace listeners.
        /// </summary>
        [Parameter(ParameterSetName = "optionsSet")]
        public TraceOptions ListenerOption
        {
            get
            {
                return base.ListenerOptionsInternal;
            }

            set
            {
                base.ListenerOptionsInternal = value;
            }
        }

        /// <summary>
        /// Adds the file trace listener using the specified file.
        /// </summary>
        /// <value></value>
        [Parameter(ParameterSetName = "optionsSet")]
        [Alias("PSPath", "Path")]
        public string FilePath
        {
            get { return base.FileListener; }

            set { base.FileListener = value; }
        }

        /// <summary>
        /// Force parameter to control read-only files.
        /// </summary>
        [Parameter(ParameterSetName = "optionsSet")]
        public SwitchParameter Force
        {
            get { return base.ForceWrite; }

            set { base.ForceWrite = value; }
        }

        /// <summary>
        /// If this parameter is specified the Debugger trace listener will be added.
        /// </summary>
        /// <value></value>
        [Parameter(ParameterSetName = "optionsSet")]
        public SwitchParameter Debugger
        {
            get { return base.DebuggerListener; }

            set { base.DebuggerListener = value; }
        }

        /// <summary>
        /// If this parameter is specified the PSHost trace listener will be added.
        /// </summary>
        /// <value></value>
        [Parameter(ParameterSetName = "optionsSet")]
        public SwitchParameter PSHost
        {
            get { return base.PSHostListener; }

            set { base.PSHostListener = value; }
        }

        /// <summary>
        /// If set, the specified listeners will be removed regardless of their type.
        /// </summary>
        [Parameter(ParameterSetName = "removeAllListenersSet")]
        [ValidateNotNullOrEmpty]
        public string[] RemoveListener { get; set; } = new string[] { "*" };

        /// <summary>
        /// If set, the specified file trace listeners will be removed.
        /// </summary>
        [Parameter(ParameterSetName = "removeFileListenersSet")]
        [ValidateNotNullOrEmpty]
        public string[] RemoveFileListener { get; set; } = new string[] { "*" };

        /// <summary>
        /// Determines if the modified PSTraceSource should be written out.
        /// Default is false.
        /// </summary>
        /// <value></value>
        [Parameter(ParameterSetName = "optionsSet")]
        public SwitchParameter PassThru
        {
            get { return _passThru; }

            set { _passThru = value; }
        }

        private bool _passThru;

        #endregion Parameters

        #region Cmdlet code

        /// <summary>
        /// Sets the TraceSource properties.
        /// </summary>
        protected override void ProcessRecord()
        {
            Collection<PSTraceSource> matchingSources = null;

            switch (ParameterSetName)
            {
                case "optionsSet":
                    Collection<PSTraceSource> preconfiguredTraceSources = null;
                    matchingSources = ConfigureTraceSource(Name, true, out preconfiguredTraceSources);

                    if (PassThru)
                    {
                        WriteObject(matchingSources, true);
                        WriteObject(preconfiguredTraceSources, true);
                    }

                    break;

                case "removeAllListenersSet":
                    matchingSources = GetMatchingTraceSource(Name, true);
                    RemoveListenersByName(matchingSources, RemoveListener, false);
                    break;

                case "removeFileListenersSet":
                    matchingSources = GetMatchingTraceSource(Name, true);
                    RemoveListenersByName(matchingSources, RemoveFileListener, true);
                    break;
            }
        }

        #endregion Cmdlet code
    }
}