bin/projects/dbatools/dbatools/Commands/SelectDbaObjectCommand.cs

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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using Sqlcollaborative.Dbatools.Parameter;

namespace Sqlcollaborative.Dbatools.Commands
{
    /// <summary>
    /// Implements the Select-DbaObject command
    /// </summary>
    [Cmdlet("Select", "DbaObject", DefaultParameterSetName = "DefaultParameter", RemotingCapability = RemotingCapability.None)]
    public class SelectDbaObjectCommand : PSCmdlet
    {
        #region Parameters
        /// <summary>
        /// The actual input object that is being processed
        /// </summary>
        [Parameter(ValueFromPipeline = true)]
        public PSObject InputObject;

        /// <summary>
        /// The properties to select. Supports fancy DSL
        /// </summary>
        [Parameter(ParameterSetName = "DefaultParameter", Position = 0)]
        [Parameter(ParameterSetName = "SkipLastParameter", Position = 0)]
        public DbaSelectParameter[] Property = new DbaSelectParameter[0];

        /// <summary>
        /// Properties to skip
        /// </summary>
        [Parameter(ParameterSetName = "DefaultParameter")]
        [Parameter(ParameterSetName = "SkipLastParameter")]
        public string[] ExcludeProperty = new string[0];

        /// <summary>
        /// A property to expand.
        /// </summary>
        [Parameter(ParameterSetName = "DefaultParameter")]
        [Parameter(ParameterSetName = "SkipLastParameter")]
        public string ExpandProperty;

        /// <summary>
        /// Whether to exclude duplicates
        /// </summary>
        [Parameter()]
        public SwitchParameter Unique;

        /// <summary>
        /// The last number of items to pick
        /// </summary>
        [Parameter(ParameterSetName = "DefaultParameter")]
        [ValidateRange(0, 2147483647)]
        public int Last;

        /// <summary>
        /// Pick the first n items.
        /// </summary>
        [Parameter(ParameterSetName = "DefaultParameter")]
        [ValidateRange(0, 2147483647)]
        public int First;

        /// <summary>
        /// Skip n items before picking items
        /// </summary>
        [Parameter(ParameterSetName = "DefaultParameter")]
        [ValidateRange(0, 2147483647)]
        public int Skip;

        /// <summary>
        /// Skip the last n items
        /// </summary>
        [Parameter(ParameterSetName = "SkipLastParameter")]
        [ValidateRange(0, 2147483647)]
        public int SkipLast;

        /// <summary>
        /// 
        /// </summary>
        [Parameter(ParameterSetName = "IndexParameter")]
        [Parameter(ParameterSetName = "DefaultParameter")]
        public SwitchParameter Wait;

        /// <summary>
        /// 
        /// </summary>
        [Parameter(ParameterSetName = "IndexParameter")]
        [ValidateRange(0, 2147483647)]
        public int[] Index;

        /// <summary>
        /// THe properties to display by default
        /// </summary>
        [Parameter()]
        public string[] ShowProperty = new string[0];

        /// <summary>
        /// The properties to NOT display by default
        /// </summary>
        [Parameter()]
        public string[] ShowExcludeProperty = new string[0];

        /// <summary>
        /// The typename to assign to the psobject
        /// </summary>
        [Parameter()]
        public string TypeName;

        /// <summary>
        /// Keep the original input object, just add to it.
        /// </summary>
        [Parameter()]
        public SwitchParameter KeepInputObject;
        #endregion Parameters

        #region Private Fields
        /// <summary>
        /// List of properties to NOT clone into the hashtable used against Select-Object
        /// </summary>
        private string[] _NonclonedProperties = new string[] { "Property", "ShowProperty", "ShowExcludeProperty", "TypeName", "KeepInputObject" };

        /// <summary>
        /// Whether some adjustments to the object need to be done or whether the Select-Object output can be simply passed through.
        /// </summary>
        private bool _NoAdjustment = true;

        /// <summary>
        /// The set controlling what properties will be shown by default
        /// </summary>
        private PSMemberInfo[] _DisplayPropertySet;

        /// <summary>
        /// THe pipeline that is wrapped around Select-Object
        /// </summary>
        private SteppablePipeline _Pipeline;
        #endregion Private Fields

        #region Command Implementation
        /// <summary>
        /// Implements the begin action of the command
        /// </summary>
        protected override void BeginProcessing()
        {
            object outBuffer;
            if (MyInvocation.BoundParameters.TryGetValue("OutBuffer", out outBuffer))
            {
                MyInvocation.BoundParameters["OutBuffer"] = 1;
            }

            Hashtable clonedBoundParameters = new Hashtable();
            foreach (string key in MyInvocation.BoundParameters.Keys)
                if (!_NonclonedProperties.Contains(key))
                    clonedBoundParameters[key] = MyInvocation.BoundParameters[key];

            if (MyInvocation.BoundParameters.ContainsKey("Property"))
                clonedBoundParameters["Property"] = Property.Select(o => o.Value).AsEnumerable().ToArray();

            if ((ShowExcludeProperty.Length > 0) || (ShowProperty.Length > 0) || (!String.IsNullOrEmpty(TypeName)) || (KeepInputObject.ToBool()))
                _NoAdjustment = false;

            if (ShowProperty.Length > 0)
                _DisplayPropertySet = new PSMemberInfo[] { new PSPropertySet("DefaultDisplayPropertySet", ShowProperty) };

            // Set the list of parameters to a variable in the caller scope, so it can be splatted
            this.SessionState.PSVariable.Set("__PSFramework_SelectParam", clonedBoundParameters);
            ScriptBlock scriptCommand = ScriptBlock.Create("Select-Object @__PSFramework_SelectParam");
            _Pipeline = scriptCommand.GetSteppablePipeline(MyInvocation.CommandOrigin);

            if (_NoAdjustment)
                _Pipeline.Begin(this);
            else
                _Pipeline.Begin(true);
        }

        /// <summary>
        /// Implements the process action of the command
        /// </summary>
        protected override void ProcessRecord()
        {
            if (_NoAdjustment)
                _Pipeline.Process(InputObject);
            else
            {
                PSObject item = PSObject.AsPSObject(_Pipeline.Process(InputObject).GetValue(0));

                if (KeepInputObject.ToBool())
                {
                    PSObject tempItem = item;
                    item = InputObject;
                    foreach (PSPropertyInfo info in tempItem.Properties.Where(o => !item.Properties.Select(n => n.Name).Contains(o.Name)))
                        item.Properties.Add(info);
                }
                if (ShowProperty.Length > 0)
                    item.Members.Add(new PSMemberSet("PSStandardMembers", _DisplayPropertySet));
                else if (ShowExcludeProperty.Length > 0)
                    item.Members.Add(new PSMemberSet("PSStandardMembers", new PSMemberInfo[] { new PSPropertySet("DefaultDisplayPropertySet", item.Properties.Select(o => o.Name).Where(o => !ShowExcludeProperty.Contains(o))) }));
                if (!String.IsNullOrEmpty(TypeName))
                    item.TypeNames.Insert(0, TypeName);
                WriteObject(item);
            }
        }

        /// <summary>
        /// Implements the end action of the command
        /// </summary>
        protected override void EndProcessing()
        {
            _Pipeline.End();
        }
        #endregion Command Implementation
    }
}