File size: 10,624 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
226
227
228
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

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

namespace Microsoft.PowerShell.Commands.Internal.Format
{
    /// <summary>
    /// Helper class to hold a resolved expression and its
    /// originating parameter.
    /// </summary>
    internal sealed class MshResolvedExpressionParameterAssociation
    {
        #region tracer
        [TraceSource("MshResolvedExpressionParameterAssociation", "MshResolvedExpressionParameterAssociation")]
        internal static readonly PSTraceSource tracer = PSTraceSource.GetTracer("MshResolvedExpressionParameterAssociation",
                                                "MshResolvedExpressionParameterAssociation");
        #endregion tracer

        internal MshResolvedExpressionParameterAssociation(MshParameter parameter, PSPropertyExpression expression)
        {
            if (expression == null)
                throw PSTraceSource.NewArgumentNullException(nameof(expression));

            OriginatingParameter = parameter;
            ResolvedExpression = expression;
        }

        internal PSPropertyExpression ResolvedExpression { get; }

        internal MshParameter OriginatingParameter { get; }
    }

    internal static class AssociationManager
    {
        internal static List<MshResolvedExpressionParameterAssociation> SetupActiveProperties(List<MshParameter> rawMshParameterList,
                                                   PSObject target, PSPropertyExpressionFactory expressionFactory)
        {
            // check if we received properties from the command line
            if (rawMshParameterList != null && rawMshParameterList.Count > 0)
            {
                return AssociationManager.ExpandParameters(rawMshParameterList, target);
            }

            // we did not get any properties:
            // try to get properties from the default property set of the object
            List<MshResolvedExpressionParameterAssociation> activeAssociationList = AssociationManager.ExpandDefaultPropertySet(target, expressionFactory);

            if (activeAssociationList.Count > 0)
            {
                // we got a valid set of properties from the default property set..add computername for
                // remoteobjects (if available)
                if (PSObjectHelper.ShouldShowComputerNameProperty(target))
                {
                    activeAssociationList.Add(new MshResolvedExpressionParameterAssociation(null,
                        new PSPropertyExpression(RemotingConstants.ComputerNameNoteProperty)));
                }

                return activeAssociationList;
            }

            // we failed to get anything from the default property set
            // just get all the properties
            activeAssociationList = AssociationManager.ExpandAll(target);
            // Remove PSComputerName and PSShowComputerName from the display as needed.
            AssociationManager.HandleComputerNameProperties(target, activeAssociationList);

            return activeAssociationList;
        }

        internal static List<MshResolvedExpressionParameterAssociation> ExpandTableParameters(List<MshParameter> parameters, PSObject target)
        {
            List<MshResolvedExpressionParameterAssociation> retVal = new List<MshResolvedExpressionParameterAssociation>();

            foreach (MshParameter par in parameters)
            {
                PSPropertyExpression expression = par.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
                List<PSPropertyExpression> expandedExpressionList = expression.ResolveNames(target);

                if (!expression.HasWildCardCharacters && expandedExpressionList.Count == 0)
                {
                    // we did not find anything, mark as unresolved
                    retVal.Add(new MshResolvedExpressionParameterAssociation(par, expression));
                }

                foreach (PSPropertyExpression ex in expandedExpressionList)
                {
                    retVal.Add(new MshResolvedExpressionParameterAssociation(par, ex));
                }
            }

            return retVal;
        }

        internal static List<MshResolvedExpressionParameterAssociation> ExpandParameters(List<MshParameter> parameters, PSObject target)
        {
            List<MshResolvedExpressionParameterAssociation> retVal = new List<MshResolvedExpressionParameterAssociation>();

            foreach (MshParameter par in parameters)
            {
                PSPropertyExpression expression = par.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
                List<PSPropertyExpression> expandedExpressionList = expression.ResolveNames(target);

                foreach (PSPropertyExpression ex in expandedExpressionList)
                {
                    retVal.Add(new MshResolvedExpressionParameterAssociation(par, ex));
                }
            }

            return retVal;
        }

        internal static List<MshResolvedExpressionParameterAssociation> ExpandDefaultPropertySet(PSObject target, PSPropertyExpressionFactory expressionFactory)
        {
            List<MshResolvedExpressionParameterAssociation> retVal = new List<MshResolvedExpressionParameterAssociation>();
            List<PSPropertyExpression> expandedExpressionList = PSObjectHelper.GetDefaultPropertySet(target);

            foreach (PSPropertyExpression ex in expandedExpressionList)
            {
                retVal.Add(new MshResolvedExpressionParameterAssociation(null, ex));
            }

            return retVal;
        }

        private static List<string> GetPropertyNamesFromView(PSObject source, PSMemberViewTypes viewType)
        {
            Collection<CollectionEntry<PSMemberInfo>> memberCollection =
                PSObject.GetMemberCollection(viewType);

            PSMemberInfoIntegratingCollection<PSMemberInfo> membersToSearch =
                new PSMemberInfoIntegratingCollection<PSMemberInfo>(source, memberCollection);

            ReadOnlyPSMemberInfoCollection<PSMemberInfo> matchedMembers =
                membersToSearch.Match("*", PSMemberTypes.Properties);

            List<string> retVal = new List<string>();
            foreach (PSMemberInfo member in matchedMembers)
            {
                retVal.Add(member.Name);
            }

            return retVal;
        }

        internal static List<MshResolvedExpressionParameterAssociation> ExpandAll(PSObject target)
        {
            List<string> adaptedProperties = GetPropertyNamesFromView(target, PSMemberViewTypes.Adapted);
            List<string> baseProperties = GetPropertyNamesFromView(target, PSMemberViewTypes.Base);
            List<string> extendedProperties = GetPropertyNamesFromView(target, PSMemberViewTypes.Extended);

            var displayedProperties = adaptedProperties.Count != 0 ? adaptedProperties : baseProperties;
            displayedProperties.AddRange(extendedProperties);

            Dictionary<string, object> duplicatesFinder = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            List<MshResolvedExpressionParameterAssociation> retVal = new List<MshResolvedExpressionParameterAssociation>();
            foreach (string property in displayedProperties)
            {
                if (duplicatesFinder.TryAdd(property, null))
                {
                    PSPropertyExpression expr = new PSPropertyExpression(property, true);
                    retVal.Add(new MshResolvedExpressionParameterAssociation(null, expr));
                }
            }

            return retVal;
        }

        /// <summary>
        /// Helper method to handle PSComputerName and PSShowComputerName properties from
        /// the formating objects. If PSShowComputerName exists and is false, removes
        /// PSComputerName from the display.
        ///
        /// PSShowComputerName is an internal property..so this property is always
        /// removed from the display.
        /// </summary>
        /// <param name="so"></param>
        /// <param name="activeAssociationList"></param>
        internal static void HandleComputerNameProperties(PSObject so, List<MshResolvedExpressionParameterAssociation> activeAssociationList)
        {
            if (so.Properties[RemotingConstants.ShowComputerNameNoteProperty] != null)
            {
                // always remove PSShowComputerName for the display. This is an internal property
                // that should never be visible to the user.
                Collection<MshResolvedExpressionParameterAssociation> itemsToRemove = new Collection<MshResolvedExpressionParameterAssociation>();
                foreach (MshResolvedExpressionParameterAssociation cpProp in activeAssociationList)
                {
                    if (cpProp.ResolvedExpression.ToString().Equals(RemotingConstants.ShowComputerNameNoteProperty,
                        StringComparison.OrdinalIgnoreCase))
                    {
                        itemsToRemove.Add(cpProp);
                        break;
                    }
                }

                // remove computername for remoteobjects..only if PSShowComputerName property exists
                // otherwise the PSComputerName property does not belong to a remote object:
                // Ex: icm $s { gps } | select pscomputername --> In this case we want to show
                // PSComputerName
                if ((so.Properties[RemotingConstants.ComputerNameNoteProperty] != null) &&
                    (!PSObjectHelper.ShouldShowComputerNameProperty(so)))
                {
                    foreach (MshResolvedExpressionParameterAssociation cpProp in activeAssociationList)
                    {
                        if (cpProp.ResolvedExpression.ToString().Equals(RemotingConstants.ComputerNameNoteProperty,
                            StringComparison.OrdinalIgnoreCase))
                        {
                            itemsToRemove.Add(cpProp);
                            break;
                        }
                    }
                }

                if (itemsToRemove.Count > 0)
                {
                    foreach (MshResolvedExpressionParameterAssociation itemToRemove in itemsToRemove)
                    {
                        activeAssociationList.Remove(itemToRemove);
                    }
                }
            }
        }
    }
}