private void AnalyzeObjectProperties(PSObject inObj)
        {
            MeasureObjectDictionary <object> dictionary = new MeasureObjectDictionary <object>();

            foreach (string str in this.Property)
            {
                MshExpression        expression = new MshExpression(str);
                List <MshExpression> list       = expression.ResolveNames(inObj);
                if ((list == null) || (list.Count == 0))
                {
                    if (!expression.HasWildCardCharacters)
                    {
                        string key = expression.ToString();
                        this.statistics.EnsureEntry(key);
                    }
                }
                else
                {
                    foreach (MshExpression expression2 in list)
                    {
                        string str3 = expression2.ToString();
                        if (!dictionary.ContainsKey(str3))
                        {
                            List <MshExpressionResult> values = expression2.GetValues(inObj);
                            if ((values != null) && (values.Count != 0))
                            {
                                this.AnalyzeValue(str3, values[0].Result);
                                dictionary[str3] = null;
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        // Expand a list of (possibly wildcarded) expressions into resolved expressions that
        // match property names on the incoming objects.
        private static void ExpandExpressions(PSObject inputObject, List <MshParameter> UnexpandedParametersWithWildCardPattern, List <MshParameter> expandedParameterList)
        {
            if (UnexpandedParametersWithWildCardPattern != null)
            {
                foreach (MshParameter unexpandedParameter in UnexpandedParametersWithWildCardPattern)
                {
                    MshExpression ex = (MshExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey);

                    SortedDictionary <string, MshExpression> expandedPropertyNames = new SortedDictionary <string, MshExpression>(StringComparer.OrdinalIgnoreCase);
                    if (inputObject == null)
                    {
                        continue;
                    }

                    foreach (MshExpression resolvedName in ex.ResolveNames(PSObject.AsPSObject(inputObject)))
                    {
                        expandedPropertyNames[resolvedName.ToString()] = resolvedName;
                    }

                    foreach (MshExpression expandedExpression in expandedPropertyNames.Values)
                    {
                        MshParameter expandedParameter = new MshParameter();
                        expandedParameter.hash = (Hashtable)unexpandedParameter.hash.Clone();
                        expandedParameter.hash[FormatParameterDefinitionKeys.ExpressionEntryKey] = expandedExpression;

                        expandedParameterList.Add(expandedParameter);
                    }
                }
            }
        }
        /// <summary>
        /// Analyze an object on a property-by-property basis instead
        /// of as a simple value.
        /// Side effects: Updates statistics.
        /// <param name="inObj">The object to analyze.</param>
        /// </summary>
        private void AnalyzeObjectProperties(PSObject inObj)
        {
            // Keep track of which properties are counted for an
            // input object so that repeated properties won't be
            // counted twice.
            MeasureObjectDictionary <object> countedProperties = new MeasureObjectDictionary <object>();

            // First iterate over the user-specified list of
            // properties...
            foreach (string p in Property)
            {
                MshExpression        expression    = new MshExpression(p);
                List <MshExpression> resolvedNames = expression.ResolveNames(inObj);
                if (resolvedNames == null || resolvedNames.Count == 0)
                {
                    // Insert a blank entry so we can track
                    // property misses in EndProcessing.
                    if (!expression.HasWildCardCharacters)
                    {
                        string propertyName = expression.ToString();
                        _statistics.EnsureEntry(propertyName);
                    }

                    continue;
                }

                // Each property value can potentially refer
                // to multiple properties via globbing. Iterate over
                // the actual property names.
                foreach (MshExpression resolvedName in resolvedNames)
                {
                    string propertyName = resolvedName.ToString();
                    // skip duplicated properties
                    if (countedProperties.ContainsKey(propertyName))
                    {
                        continue;
                    }

                    List <MshExpressionResult> tempExprRes = resolvedName.GetValues(inObj);
                    if (tempExprRes == null || tempExprRes.Count == 0)
                    {
                        // Shouldn't happen - would somehow mean
                        // that the property went away between when
                        // we resolved it and when we tried to get its
                        // value.
                        continue;
                    }

                    AnalyzeValue(propertyName, tempExprRes[0].Result);

                    // Remember resolved propertyNames that have been counted
                    countedProperties[propertyName] = null;
                }
            }
        }
        private void ProcessParameter(MshParameter p, PSObject inputObject, List <PSNoteProperty> result)
        {
            string        entry             = p.GetEntry("name") as string;
            MshExpression re                = p.GetEntry("expression") as MshExpression;
            List <MshExpressionResult> list = new List <MshExpressionResult>();

            foreach (MshExpression expression2 in re.ResolveNames(inputObject))
            {
                if ((this.exclusionFilter == null) || !this.exclusionFilter.IsMatch(expression2))
                {
                    List <MshExpressionResult> values = expression2.GetValues(inputObject);
                    if (values != null)
                    {
                        foreach (MshExpressionResult result2 in values)
                        {
                            list.Add(result2);
                        }
                    }
                }
            }
            if (list.Count == 0)
            {
                list.Add(new MshExpressionResult(null, re, null));
            }
            else if (!string.IsNullOrEmpty(entry) && (list.Count > 1))
            {
                ErrorRecord errorRecord = new ErrorRecord(new InvalidOperationException(SelectObjectStrings.RenamingMultipleResults), "RenamingMultipleResults", ErrorCategory.InvalidOperation, inputObject);
                base.WriteError(errorRecord);
                return;
            }
            foreach (MshExpressionResult result3 in list)
            {
                if ((this.exclusionFilter == null) || !this.exclusionFilter.IsMatch(result3.ResolvedExpression))
                {
                    PSNoteProperty property;
                    if (string.IsNullOrEmpty(entry))
                    {
                        string str3 = result3.ResolvedExpression.ToString();
                        if (string.IsNullOrEmpty(str3))
                        {
                            PSArgumentException exception = PSTraceSource.NewArgumentException("Property", this.ResourcesBaseName, "EmptyScriptBlockAndNoName", new object[0]);
                            base.ThrowTerminatingError(new ErrorRecord(exception, "EmptyScriptBlockAndNoName", ErrorCategory.InvalidArgument, null));
                        }
                        property = new PSNoteProperty(str3, result3.Result);
                    }
                    else
                    {
                        property = new PSNoteProperty(entry, result3.Result);
                    }
                    result.Add(property);
                }
            }
        }
Exemple #5
0
        // Expand a list of (possibly wildcarded) expressions into resolved expressions that
        // match property names on the incoming objects.
        private static List <MshParameter> ExpandExpressions(List <PSObject> inputObjects, List <MshParameter> unexpandedParameterList)
        {
            List <MshParameter> expandedParameterList = new List <MshParameter>();

            if (unexpandedParameterList != null)
            {
                foreach (MshParameter unexpandedParameter in unexpandedParameterList)
                {
                    MshExpression ex = (MshExpression)unexpandedParameter.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey);
                    if (!ex.HasWildCardCharacters) // this special cases 1) script blocks and 2) wildcard-less strings
                    {
                        expandedParameterList.Add(unexpandedParameter);
                    }
                    else
                    {
                        SortedDictionary <string, MshExpression> expandedPropertyNames = new SortedDictionary <string, MshExpression>(StringComparer.OrdinalIgnoreCase);
                        if (inputObjects != null)
                        {
                            foreach (object inputObject in inputObjects)
                            {
                                if (inputObject == null)
                                {
                                    continue;
                                }

                                foreach (MshExpression resolvedName in ex.ResolveNames(PSObject.AsPSObject(inputObject)))
                                {
                                    expandedPropertyNames[resolvedName.ToString()] = resolvedName;
                                }
                            }
                        }

                        foreach (MshExpression expandedExpression in expandedPropertyNames.Values)
                        {
                            MshParameter expandedParameter = new MshParameter();
                            expandedParameter.hash = (Hashtable)unexpandedParameter.hash.Clone();
                            expandedParameter.hash[FormatParameterDefinitionKeys.ExpressionEntryKey] = expandedExpression;

                            expandedParameterList.Add(expandedParameter);
                        }
                    }
                }
            }

            return(expandedParameterList);
        }
        private static List <MshParameter> ExpandExpressions(List <PSObject> inputObjects, List <MshParameter> unexpandedParameterList)
        {
            List <MshParameter> list = new List <MshParameter>();

            if (unexpandedParameterList != null)
            {
                foreach (MshParameter parameter in unexpandedParameterList)
                {
                    MshExpression entry = (MshExpression)parameter.GetEntry("expression");
                    if (!entry.HasWildCardCharacters)
                    {
                        list.Add(parameter);
                    }
                    else
                    {
                        SortedDictionary <string, MshExpression> dictionary = new SortedDictionary <string, MshExpression>(StringComparer.OrdinalIgnoreCase);
                        if (inputObjects != null)
                        {
                            foreach (object obj2 in inputObjects)
                            {
                                if (obj2 != null)
                                {
                                    foreach (MshExpression expression2 in entry.ResolveNames(PSObject.AsPSObject(obj2)))
                                    {
                                        dictionary[expression2.ToString()] = expression2;
                                    }
                                }
                            }
                        }
                        foreach (MshExpression expression3 in dictionary.Values)
                        {
                            MshParameter item = new MshParameter {
                                hash = (Hashtable)parameter.hash.Clone()
                            };
                            item.hash["expression"] = expression3;
                            list.Add(item);
                        }
                    }
                }
            }
            return(list);
        }
        /// <summary>
        /// Resolve all wildcards in user input Property into resolvedNameMshParameters
        /// </summary>
        private void InitializeResolvedNameMshParameters()
        {
            // temp list of properties with wildcards resolved
            ArrayList resolvedNameProperty = new ArrayList();

            foreach (MshParameter p in _propertyMshParameterList)
            {
                string               label         = p.GetEntry(ConvertHTMLParameterDefinitionKeys.LabelEntryKey) as string;
                string               alignment     = p.GetEntry(ConvertHTMLParameterDefinitionKeys.AlignmentEntryKey) as string;
                string               width         = p.GetEntry(ConvertHTMLParameterDefinitionKeys.WidthEntryKey) as string;
                MshExpression        ex            = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression;
                List <MshExpression> resolvedNames = ex.ResolveNames(_inputObject);
                foreach (MshExpression resolvedName in resolvedNames)
                {
                    Hashtable ht = CreateAuxPropertyHT(label, alignment, width);
                    ht.Add(FormatParameterDefinitionKeys.ExpressionEntryKey, resolvedName.ToString());
                    resolvedNameProperty.Add(ht);
                }
            }
            _resolvedNameMshParameters = ProcessParameter(resolvedNameProperty.ToArray());
        }
Exemple #8
0
        private void InitializeResolvedNameMshParameters()
        {
            ArrayList list = new ArrayList();

            foreach (MshParameter parameter in this.propertyMshParameterList)
            {
                string               entry      = parameter.GetEntry("label") as string;
                string               alignment  = parameter.GetEntry("alignment") as string;
                string               width      = parameter.GetEntry("width") as string;
                MshExpression        expression = parameter.GetEntry("expression") as MshExpression;
                List <MshExpression> list2      = expression.ResolveNames(this.inputObject);
                if (list2.Count == 1)
                {
                    Hashtable hashtable = CreateAuxPropertyHT(entry, alignment, width);
                    if (expression.Script != null)
                    {
                        hashtable.Add("expression", expression.Script);
                    }
                    else
                    {
                        hashtable.Add("expression", expression.ToString());
                    }
                    list.Add(hashtable);
                }
                else
                {
                    foreach (MshExpression expression2 in list2)
                    {
                        Hashtable hashtable2 = CreateAuxPropertyHT(entry, alignment, width);
                        hashtable2.Add("expression", expression2.ToString());
                        list.Add(hashtable2);
                    }
                }
            }
            this.resolvedNameMshParameters = this.ProcessParameter(list.ToArray());
        }
Exemple #9
0
        private void ProcessParameter(MshParameter p, PSObject inputObject, List <PSNoteProperty> result)
        {
            string name = p.GetEntry(NameEntryDefinition.NameEntryKey) as string;

            MshExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as MshExpression;
            List <MshExpressionResult> expressionResults = new List <MshExpressionResult>();

            foreach (MshExpression resolvedName in ex.ResolveNames(inputObject))
            {
                if (_exclusionFilter == null || !_exclusionFilter.IsMatch(resolvedName))
                {
                    List <MshExpressionResult> tempExprResults = resolvedName.GetValues(inputObject);
                    if (tempExprResults == null)
                    {
                        continue;
                    }
                    foreach (MshExpressionResult mshExpRes in tempExprResults)
                    {
                        expressionResults.Add(mshExpRes);
                    }
                }
            }

            // allow 'Select-Object -Property noexist-name' to return a PSObject with property noexist-name,
            // unless noexist-name itself contains wildcards
            if (expressionResults.Count == 0 && !ex.HasWildCardCharacters)
            {
                expressionResults.Add(new MshExpressionResult(null, ex, null));
            }

            // if we have an expansion, renaming is not acceptable
            else if (!string.IsNullOrEmpty(name) && expressionResults.Count > 1)
            {
                string      errorMsg    = SelectObjectStrings.RenamingMultipleResults;
                ErrorRecord errorRecord = new ErrorRecord(
                    new InvalidOperationException(errorMsg),
                    "RenamingMultipleResults",
                    ErrorCategory.InvalidOperation,
                    inputObject);
                WriteError(errorRecord);
                return;
            }

            foreach (MshExpressionResult r in expressionResults)
            {
                // filter the exclusions, if any
                if (_exclusionFilter != null && _exclusionFilter.IsMatch(r.ResolvedExpression))
                {
                    continue;
                }

                PSNoteProperty mshProp;
                if (string.IsNullOrEmpty(name))
                {
                    string resolvedExpressionName = r.ResolvedExpression.ToString();
                    if (string.IsNullOrEmpty(resolvedExpressionName))
                    {
                        PSArgumentException mshArgE = PSTraceSource.NewArgumentException(
                            "Property",
                            SelectObjectStrings.EmptyScriptBlockAndNoName);
                        ThrowTerminatingError(
                            new ErrorRecord(
                                mshArgE,
                                "EmptyScriptBlockAndNoName",
                                ErrorCategory.InvalidArgument, null));
                    }
                    mshProp = new PSNoteProperty(resolvedExpressionName, r.Result);
                }
                else
                {
                    mshProp = new PSNoteProperty(name, r.Result);
                }
                result.Add(mshProp);
            }
        }