Пример #1
0
        /// <summary>
        /// helper to retrieve the value of an PSPropertyExpression and to format it
        /// </summary>
        /// <param name="so">shell object to process</param>
        /// <param name="enumerationLimit">limit on IEnumerable enumeration</param>
        /// <param name="ex">expression to use for retrieval</param>
        /// <param name="directive">format directive to use for formatting</param>
        /// <param name="formatErrorObject"></param>
        /// <param name="expressionFactory">expression factory to create PSPropertyExpression</param>
        /// <param name="result"> not null if an error condition arose</param>
        /// <returns>formatted string</returns>
        internal static string GetExpressionDisplayValue(
            PSObject so,
            int enumerationLimit,
            PSPropertyExpression ex,
            FieldFormattingDirective directive,
            StringFormatError formatErrorObject,
            PSPropertyExpressionFactory expressionFactory,
            out PSPropertyExpressionResult result)
        {
            result = null;
            List <PSPropertyExpressionResult> resList = ex.GetValues(so);

            if (resList.Count == 0)
            {
                return(string.Empty);
            }

            result = resList[0];
            if (result.Exception != null)
            {
                return(string.Empty);
            }

            return(PSObjectHelper.FormatField(directive, result.Result, enumerationLimit, formatErrorObject, expressionFactory));
        }
Пример #2
0
        /// <summary>
        /// log a failed evaluation of an PSPropertyExpression.
        /// </summary>
        /// <param name="result">PSPropertyExpressionResult containing the failed evaluation data.</param>
        /// <param name="sourceObject">Object used to evaluate the PSPropertyExpression.</param>
        internal void LogPSPropertyExpressionFailedResult(PSPropertyExpressionResult result, object sourceObject)
        {
            if (!_formatErrorPolicy.ShowErrorsAsMessages)
            {
                return;
            }
            PSPropertyExpressionError error = new PSPropertyExpressionError();

            error.result       = result;
            error.sourceObject = sourceObject;
            _formattingErrorList.Add(error);
        }
Пример #3
0
        private static string GetSmartToStringDisplayName(object x, PSPropertyExpressionFactory expressionFactory)
        {
            PSPropertyExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory);

            if ((r != null) && (r.Exception == null))
            {
                return(PSObjectHelper.AsPSObject(r.Result).ToString());
            }
            else
            {
                return(PSObjectHelper.AsPSObject(x).ToString());
            }
        }
Пример #4
0
        private static string GetObjectName(object x, PSPropertyExpressionFactory expressionFactory)
        {
            string objName;

            // check if the underlying object is of primitive type
            // if so just return its value
            if (x is PSObject &&
                (LanguagePrimitives.IsBoolOrSwitchParameterType((((PSObject)x).BaseObject).GetType()) ||
                 LanguagePrimitives.IsNumeric(((((PSObject)x).BaseObject).GetType()).GetTypeCode()) ||
                 LanguagePrimitives.IsNull(x)))
            {
                objName = x.ToString();
            }
            else if (x == null)
            {
                // use PowerShell's $null variable to indicate that the value is null...
                objName = "$null";
            }
            else
            {
                MethodInfo toStringMethod = x.GetType().GetMethod("ToString", PSTypeExtensions.EmptyTypes);
                // TODO:CORECLR double check with CORE CLR that x.GetType() == toStringMethod.ReflectedType
                // Check if the given object "x" implements "toString" method. Do that by comparing "DeclaringType" which 'Gets the class that declares this member' and the object type
                if (toStringMethod.DeclaringType == x.GetType())
                {
                    objName = PSObjectHelper.AsPSObject(x).ToString();
                }
                else
                {
                    PSPropertyExpressionResult r = PSObjectHelper.GetDisplayName(PSObjectHelper.AsPSObject(x), expressionFactory);
                    if ((r != null) && (r.Exception == null))
                    {
                        objName = PSObjectHelper.AsPSObject(r.Result).ToString();;
                    }
                    else
                    {
                        objName = PSObjectHelper.AsPSObject(x).ToString();
                        if (objName == string.Empty)
                        {
                            var baseObj = PSObject.Base(x);
                            if (baseObj != null)
                            {
                                objName = baseObj.ToString();
                            }
                        }
                    }
                }
            }

            return(objName);
        }
Пример #5
0
        internal static bool Evaluate(PSObject obj, PSPropertyExpression ex, out PSPropertyExpressionResult expressionResult)
        {
            expressionResult = null;
            List <PSPropertyExpressionResult> res = ex.GetValues(obj);

            if (res.Count == 0)
            {
                return(false);
            }
            if (res[0].Exception != null)
            {
                expressionResult = res[0];
                return(false);
            }
            return(LanguagePrimitives.IsTrue(res[0].Result));
        }
        internal override object GetValue(PSObject liveObject)
        {
            List <PSPropertyExpressionResult> resList = _expression.GetValues(liveObject);

            if (resList.Count == 0)
            {
                return(null);
            }

            // Only first element is used.
            PSPropertyExpressionResult result = resList[0];

            if (result.Exception != null)
            {
                return(null);
            }

            object objectResult = result.Result;

            return(objectResult == null ? string.Empty : ColumnInfo.LimitString(objectResult.ToString()));
        }
Пример #7
0
        private void ProcessActiveAssociationList(PSObject so,
                                                  TraversalInfo currentLevel,
                                                  List <MshResolvedExpressionParameterAssociation> activeAssociationList,
                                                  List <FormatValue> formatValueList)
        {
            foreach (MshResolvedExpressionParameterAssociation a in activeAssociationList)
            {
                FormatTextField ftf = new FormatTextField();

                ftf.text = a.ResolvedExpression.ToString() + " = ";
                formatValueList.Add(ftf);

                // compute the value of the entry
                List <PSPropertyExpressionResult> resList = a.ResolvedExpression.GetValues(so);
                object val = null;
                if (resList.Count >= 1)
                {
                    PSPropertyExpressionResult result = resList[0];
                    if (result.Exception != null)
                    {
                        _errorManager.LogPSPropertyExpressionFailedResult(result, so);
                        if (_errorManager.DisplayErrorStrings)
                        {
                            val = _errorManager.ErrorString;
                        }
                        else
                        {
                            val = string.Empty;
                        }
                    }
                    else
                    {
                        val = result.Result;
                    }
                }

                // extract the optional max depth
                TraversalInfo level = currentLevel;
                if (a.OriginatingParameter != null)
                {
                    object maxDepthKey = a.OriginatingParameter.GetEntry(FormatParameterDefinitionKeys.DepthEntryKey);
                    if (maxDepthKey != AutomationNull.Value)
                    {
                        int parameterMaxDept = (int)maxDepthKey;
                        level = new TraversalInfo(currentLevel.Level, parameterMaxDept);
                    }
                }

                IEnumerable e = null;
                if (val != null || (level.Level >= level.MaxDepth))
                {
                    e = PSObjectHelper.GetEnumerable(val);
                }

                if (e != null)
                {
                    formatValueList.Add(new FormatNewLine());
                    DisplayEnumeration(e, level.NextLevel, AddIndentationLevel(formatValueList));
                }
                else if (val == null || TreatAsLeafNode(val, level))
                {
                    DisplayLeaf(val, formatValueList);
                }
                else
                {
                    formatValueList.Add(new FormatNewLine());

                    // we need to go one more level down
                    DisplayObject(PSObject.AsPSObject(val), level.NextLevel, null,
                                  AddIndentationLevel(formatValueList));
                }
            } // for each
        }
Пример #8
0
        protected FormatPropertyField GenerateFormatPropertyField(List <FormatToken> formatTokenList, PSObject so, int enumerationLimit, out PSPropertyExpressionResult result)
        {
            result = null;
            FormatPropertyField fpf = new FormatPropertyField();

            if (formatTokenList.Count != 0)
            {
                FormatToken        token = formatTokenList[0];
                FieldPropertyToken fpt   = token as FieldPropertyToken;
                if (fpt != null)
                {
                    PSPropertyExpression ex = this.expressionFactory.CreateFromExpressionToken(fpt.expression, this.dataBaseInfo.view.loadingInfo);
                    fpf.propertyValue = this.GetExpressionDisplayValue(so, enumerationLimit, ex, fpt.fieldFormattingDirective, out result);
                }
                else
                {
                    TextToken tt = token as TextToken;
                    if (tt != null)
                    {
                        fpf.propertyValue = this.dataBaseInfo.db.displayResourceManagerCache.GetTextTokenString(tt);
                    }
                }
            }
            else
            {
                fpf.propertyValue = string.Empty;
            }

            return(fpf);
        }
Пример #9
0
        protected string GetExpressionDisplayValue(PSObject so, int enumerationLimit, PSPropertyExpression ex,
                                                   FieldFormattingDirective directive, out PSPropertyExpressionResult expressionResult)
        {
            StringFormatError formatErrorObject = null;

            if (_errorManager.DisplayFormatErrorString)
            {
                // we send a format error object down to the formatting calls
                // only if we want to show the formatting error strings
                formatErrorObject = new StringFormatError();
            }

            string retVal = PSObjectHelper.GetExpressionDisplayValue(so, enumerationLimit, ex,
                                                                     directive, formatErrorObject, expressionFactory, out expressionResult);

            if (expressionResult != null)
            {
                // we obtained a result, check if there is an error
                if (expressionResult.Exception != null)
                {
                    _errorManager.LogPSPropertyExpressionFailedResult(expressionResult, so);
                    if (_errorManager.DisplayErrorStrings)
                    {
                        retVal = _errorManager.ErrorString;
                    }
                }
                else if (formatErrorObject != null && formatErrorObject.exception != null)
                {
                    // if we did no thave any errors in the expression evaluation
                    // we might have errors in the formatting, if present
                    _errorManager.LogStringFormatError(formatErrorObject);
                    if (_errorManager.DisplayErrorStrings)
                    {
                        retVal = _errorManager.FormatErrorString;
                    }
                }
            }

            return(retVal);
        }
Пример #10
0
        private void ProcessExpandParameter(MshParameter p, PSObject inputObject,
                                            List <PSNoteProperty> matchedProperties)
        {
            PSPropertyExpression ex = p.GetEntry(FormatParameterDefinitionKeys.ExpressionEntryKey) as PSPropertyExpression;
            List <PSPropertyExpressionResult> expressionResults = ex.GetValues(inputObject);

            if (expressionResults.Count == 0)
            {
                ErrorRecord errorRecord = new(
                    PSTraceSource.NewArgumentException("ExpandProperty", SelectObjectStrings.PropertyNotFound, ExpandProperty),
                    "ExpandPropertyNotFound",
                    ErrorCategory.InvalidArgument,
                    inputObject);
                throw new SelectObjectException(errorRecord);
            }

            if (expressionResults.Count > 1)
            {
                ErrorRecord errorRecord = new(
                    PSTraceSource.NewArgumentException("ExpandProperty", SelectObjectStrings.MutlipleExpandProperties, ExpandProperty),
                    "MutlipleExpandProperties",
                    ErrorCategory.InvalidArgument,
                    inputObject);
                throw new SelectObjectException(errorRecord);
            }

            PSPropertyExpressionResult r = expressionResults[0];

            if (r.Exception == null)
            {
                // ignore the property value if it's null
                if (r.Result == null)
                {
                    return;
                }

                System.Collections.IEnumerable results = LanguagePrimitives.GetEnumerable(r.Result);
                if (results == null)
                {
                    // add NoteProperties if there is any
                    // If r.Result is a base object, we don't want to associate the NoteProperty
                    // directly with it. We want the NoteProperty to be associated only with this
                    // particular PSObject, so that when the user uses the base object else where,
                    // its members remain the same as before the Select-Object command run.
                    PSObject expandedObject = PSObject.AsPSObject(r.Result, true);
                    AddNoteProperties(expandedObject, inputObject, matchedProperties);

                    FilteredWriteObject(expandedObject, matchedProperties);
                    return;
                }

                foreach (object expandedValue in results)
                {
                    // ignore the element if it's null
                    if (expandedValue == null)
                    {
                        continue;
                    }

                    // add NoteProperties if there is any
                    // If expandedValue is a base object, we don't want to associate the NoteProperty
                    // directly with it. We want the NoteProperty to be associated only with this
                    // particular PSObject, so that when the user uses the base object else where,
                    // its members remain the same as before the Select-Object command run.
                    PSObject expandedObject = PSObject.AsPSObject(expandedValue, true);
                    AddNoteProperties(expandedObject, inputObject, matchedProperties);

                    FilteredWriteObject(expandedObject, matchedProperties);
                }
            }
            else
            {
                ErrorRecord errorRecord = new(
                    r.Exception,
                    "PropertyEvaluationExpand",
                    ErrorCategory.InvalidResult,
                    inputObject);
                throw new SelectObjectException(errorRecord);
            }
        }