Пример #1
0
        internal static object DecodePropertyValue(PSObject psObject, string propertyName, Type propertyValueType)
        {
            ReadOnlyPSMemberInfoCollection <PSPropertyInfo> infos = psObject.Properties.Match(propertyName);

            if (infos.Count == 0)
            {
                return(null);
            }
            return(DecodeObject(infos[0].Value, propertyValueType));
        }
Пример #2
0
        /// <summary>
        /// Decode property value.
        /// </summary>
        internal static object DecodePropertyValue(PSObject psObject, string propertyName, Type propertyValueType)
        {
            Dbg.Assert(psObject != null, "Expected psObject != null");
            Dbg.Assert(propertyName != null, "Expected propertyName != null");
            Dbg.Assert(propertyValueType != null, "Expected propertyValueType != null");
            ReadOnlyPSMemberInfoCollection <PSPropertyInfo> matches = psObject.Properties.Match(propertyName);

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

            Dbg.Assert(matches.Count == 1, "Expected matches.Count == 1");
            return(DecodeObject(matches[0].Value, propertyValueType));
        }
Пример #3
0
        private void MethodCallWithArguments()
        {
            ReadOnlyPSMemberInfoCollection <PSMemberInfo> infos = this._inputObject.Members.Match(this._propertyOrMethodName, PSMemberTypes.ParameterizedProperty | PSMemberTypes.Methods);

            if (infos.Count > 1)
            {
                StringBuilder builder = new StringBuilder();
                foreach (PSMemberInfo info in infos)
                {
                    builder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[] { info.Name });
                }
                base.WriteError(GenerateNameParameterError("Name", InternalCommandStrings.AmbiguousMethodName, "AmbiguousMethodName", this._inputObject, new object[] { this._propertyOrMethodName, builder }));
            }
            else if ((infos.Count == 0) || !(infos[0] is PSMethodInfo))
            {
                base.WriteError(GenerateNameParameterError("Name", InternalCommandStrings.MethodNotFound, "MethodNotFound", this._inputObject, new object[] { this._propertyOrMethodName }));
            }
            else
            {
                PSMethodInfo  info2    = infos[0] as PSMethodInfo;
                StringBuilder builder2 = new StringBuilder(GetStringRepresentation(this._arguments[0]));
                for (int i = 1; i < this._arguments.Length; i++)
                {
                    builder2.AppendFormat(CultureInfo.InvariantCulture, ", {0}", new object[] { GetStringRepresentation(this._arguments[i]) });
                }
                string action = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectMethodActionWithArguments, new object[] { info2.Name, builder2 });
                try
                {
                    if (base.ShouldProcess(this.targetString, action) && !this.BlockMethodInLanguageMode(this.InputObject))
                    {
                        object obj2 = info2.Invoke(this._arguments);
                        this.WriteToPipelineWithUnrolling(obj2);
                    }
                }
                catch (PipelineStoppedException)
                {
                    throw;
                }
                catch (Exception exception)
                {
                    CommandProcessorBase.CheckForSevereException(exception);
                    base.WriteError(new ErrorRecord(exception, "MethodInvocationError", ErrorCategory.InvalidOperation, this._inputObject));
                }
            }
        }
        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;
        }
Пример #5
0
        internal List <MshExpression> ResolveNames(PSObject target, bool expand)
        {
            List <MshExpression> retVal = new List <MshExpression>();

            if (_isResolved)
            {
                retVal.Add(this);
                return(retVal);
            }

            if (Script != null)
            {
                // script block, just add it to the list and be done
                MshExpression ex = new MshExpression(Script);

                ex._isResolved = true;
                retVal.Add(ex);
                return(retVal);
            }

            // we have a string value
            IEnumerable <PSMemberInfo> members = null;

            if (HasWildCardCharacters)
            {
                // get the members first: this will expand the globbing on each parameter
                members = target.Members.Match(_stringValue,
                                               PSMemberTypes.Properties | PSMemberTypes.PropertySet);
            }
            else
            {
                // we have no globbing: try an exact match, because this is quicker.
                PSMemberInfo x = target.Members[_stringValue];

                List <PSMemberInfo> temp = new List <PSMemberInfo>();
                if (x != null)
                {
                    temp.Add(x);
                }
                members = temp;
            }

            // we now have a list of members, we have to expand property sets
            // and remove duplicates
            List <PSMemberInfo> temporaryMemberList = new List <PSMemberInfo>();

            foreach (PSMemberInfo member in members)
            {
                // it can be a property set
                PSPropertySet propertySet = member as PSPropertySet;
                if (propertySet != null)
                {
                    if (expand)
                    {
                        // NOTE: we expand the property set under the
                        // assumption that it contains property names that
                        // do not require any further expansion
                        Collection <string> references = propertySet.ReferencedPropertyNames;

                        for (int j = 0; j < references.Count; j++)
                        {
                            ReadOnlyPSMemberInfoCollection <PSPropertyInfo> propertyMembers =
                                target.Properties.Match(references[j]);
                            for (int jj = 0; jj < propertyMembers.Count; jj++)
                            {
                                temporaryMemberList.Add(propertyMembers[jj]);
                            }
                        }
                    }
                    continue;
                }
                // it can be a property
                if (member is PSPropertyInfo)
                {
                    temporaryMemberList.Add(member);
                }
            }

            Hashtable hash = new Hashtable();

            // build the list of unique values: remove the possible duplicates
            // from property set expansion
            foreach (PSMemberInfo m in temporaryMemberList)
            {
                if (!hash.ContainsKey(m.Name))
                {
                    MshExpression ex = new MshExpression(m.Name);

                    ex._isResolved = true;
                    retVal.Add(ex);
                    hash.Add(m.Name, null);
                }
            }

            return(retVal);
        }
Пример #6
0
        /// <summary>
        /// Resolve the names matched by this the expression.
        /// </summary>
        /// <param name="target">The object to apply the expression against.</param>
        /// <param name="expand">If the matched properties are property sets, expand them.</param>
        public List <PSPropertyExpression> ResolveNames(PSObject target, bool expand)
        {
            List <PSPropertyExpression> retVal = new List <PSPropertyExpression>();

            if (_isResolved)
            {
                retVal.Add(this);
                return(retVal);
            }

            if (Script != null)
            {
                // script block, just add it to the list and be done
                PSPropertyExpression ex = new PSPropertyExpression(Script);

                ex._isResolved = true;
                retVal.Add(ex);
                return(retVal);
            }

            // If the object passed in is a hashtable, then turn it into a PSCustomObject so
            // that property expressions can work on it.
            target = IfHashtableWrapAsPSCustomObject(target);

            // we have a string value
            IEnumerable <PSMemberInfo> members = null;

            if (HasWildCardCharacters)
            {
                // get the members first: this will expand the globbing on each parameter
                members = target.Members.Match(_stringValue,
                                               PSMemberTypes.Properties | PSMemberTypes.PropertySet | PSMemberTypes.Dynamic);
            }
            else
            {
                // we have no globbing: try an exact match, because this is quicker.
                PSMemberInfo x = target.Members[_stringValue];

                if ((x == null) && (target.BaseObject is System.Dynamic.IDynamicMetaObjectProvider))
                {
                    // We could check if GetDynamicMemberNames includes the name...  but
                    // GetDynamicMemberNames is only a hint, not a contract, so we'd want
                    // to attempt the binding whether it's in there or not.
                    x = new PSDynamicMember(_stringValue);
                }
                List <PSMemberInfo> temp = new List <PSMemberInfo>();
                if (x != null)
                {
                    temp.Add(x);
                }
                members = temp;
            }

            // we now have a list of members, we have to expand property sets
            // and remove duplicates
            List <PSMemberInfo> temporaryMemberList = new List <PSMemberInfo>();

            foreach (PSMemberInfo member in members)
            {
                // it can be a property set
                PSPropertySet propertySet = member as PSPropertySet;
                if (propertySet != null)
                {
                    if (expand)
                    {
                        // NOTE: we expand the property set under the
                        // assumption that it contains property names that
                        // do not require any further expansion
                        Collection <string> references = propertySet.ReferencedPropertyNames;

                        for (int j = 0; j < references.Count; j++)
                        {
                            ReadOnlyPSMemberInfoCollection <PSPropertyInfo> propertyMembers =
                                target.Properties.Match(references[j]);
                            for (int jj = 0; jj < propertyMembers.Count; jj++)
                            {
                                temporaryMemberList.Add(propertyMembers[jj]);
                            }
                        }
                    }
                }
                // it can be a property
                else if (member is PSPropertyInfo)
                {
                    temporaryMemberList.Add(member);
                }
                // it can be a dynamic member
                else if (member is PSDynamicMember)
                {
                    temporaryMemberList.Add(member);
                }
            }

            Hashtable hash = new Hashtable();

            // build the list of unique values: remove the possible duplicates
            // from property set expansion
            foreach (PSMemberInfo m in temporaryMemberList)
            {
                if (!hash.ContainsKey(m.Name))
                {
                    PSPropertyExpression ex = new PSPropertyExpression(m.Name);

                    ex._isResolved = true;
                    retVal.Add(ex);
                    hash.Add(m.Name, null);
                }
            }

            return(retVal);
        }
Пример #7
0
 protected override void ProcessRecord()
 {
     if ((this.InputObject != null) && (this.InputObject != AutomationNull.Value))
     {
         string  fullName;
         Type    type = null;
         Adapter dotNetStaticAdapter = null;
         if (this.Static == 1)
         {
             dotNetStaticAdapter = PSObject.dotNetStaticAdapter;
             object baseObject = this.InputObject.BaseObject;
             type = baseObject as Type;
             if (type == null)
             {
                 type = baseObject.GetType();
             }
             fullName = type.FullName;
         }
         else
         {
             ConsolidatedString internalTypeNames = this.InputObject.InternalTypeNames;
             if (internalTypeNames.Count != 0)
             {
                 fullName = internalTypeNames[0];
             }
             else
             {
                 fullName = "<null>";
             }
         }
         if (!this.typesAlreadyDisplayed.Contains(fullName))
         {
             PSMemberInfoCollection <PSMemberInfo> infos;
             this.typesAlreadyDisplayed.Add(fullName, "");
             PSMemberTypes     memberType = this.memberType;
             PSMemberViewTypes view       = this.view;
             if (((this.view & PSMemberViewTypes.Extended) == 0) && !typeof(PSMemberSet).ToString().Equals(fullName, StringComparison.OrdinalIgnoreCase))
             {
                 memberType ^= PSMemberTypes.MemberSet | PSMemberTypes.ScriptMethod | PSMemberTypes.CodeMethod | PSMemberTypes.PropertySet | PSMemberTypes.ScriptProperty | PSMemberTypes.NoteProperty | PSMemberTypes.CodeProperty | PSMemberTypes.AliasProperty;
             }
             if (((this.view & PSMemberViewTypes.Adapted) == 0) && ((this.view & PSMemberViewTypes.Base) == 0))
             {
                 memberType ^= PSMemberTypes.ParameterizedProperty | PSMemberTypes.Method | PSMemberTypes.Property;
             }
             if (((this.view & PSMemberViewTypes.Base) == PSMemberViewTypes.Base) && (this.InputObject.InternalBaseDotNetAdapter == null))
             {
                 view |= PSMemberViewTypes.Adapted;
             }
             if (this.Static == 1)
             {
                 infos = dotNetStaticAdapter.BaseGetMembers <PSMemberInfo>(type);
             }
             else
             {
                 Collection <CollectionEntry <PSMemberInfo> > memberCollection = PSObject.GetMemberCollection(view);
                 infos = new PSMemberInfoIntegratingCollection <PSMemberInfo>(this.InputObject, memberCollection);
             }
             foreach (string str3 in this.Name)
             {
                 ReadOnlyPSMemberInfoCollection <PSMemberInfo> infos2 = infos.Match(str3, memberType, this.matchOptions);
                 MemberDefinition[] array = new MemberDefinition[infos2.Count];
                 int index = 0;
                 foreach (PSMemberInfo info in infos2)
                 {
                     if (this.Force == 0)
                     {
                         PSMethod method = info as PSMethod;
                         if ((method != null) && method.IsSpecial)
                         {
                             continue;
                         }
                     }
                     array[index] = new MemberDefinition(fullName, info.Name, info.MemberType, info.ToString());
                     index++;
                 }
                 Array.Sort <MemberDefinition>(array, 0, index, new MemberComparer());
                 for (int i = 0; i < index; i++)
                 {
                     base.WriteObject(array[i]);
                 }
             }
         }
     }
 }
Пример #8
0
        private object GetValue(ref bool error)
        {
            if (LanguagePrimitives.IsNull(this.InputObject))
            {
                if (base.Context.IsStrictVersion(2))
                {
                    base.WriteError(ForEachObjectCommand.GenerateNameParameterError("InputObject", InternalCommandStrings.InputObjectIsNull, "InputObjectIsNull", this._inputObject, new object[] { this._property }));
                    error = true;
                }
                return(null);
            }
            IDictionary dictionary = PSObject.Base(this._inputObject) as IDictionary;

            try
            {
                if ((dictionary != null) && dictionary.Contains(this._property))
                {
                    return(dictionary[this._property]);
                }
            }
            catch (InvalidOperationException)
            {
            }
            ReadOnlyPSMemberInfoCollection <PSMemberInfo> matchMembers = this.GetMatchMembers();

            if (matchMembers.Count > 1)
            {
                StringBuilder builder = new StringBuilder();
                foreach (PSMemberInfo info in matchMembers)
                {
                    builder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[] { info.Name });
                }
                base.WriteError(ForEachObjectCommand.GenerateNameParameterError("Property", InternalCommandStrings.AmbiguousPropertyOrMethodName, "AmbiguousPropertyName", this._inputObject, new object[] { this._property, builder }));
                error = true;
            }
            else if (matchMembers.Count == 0)
            {
                if (base.Context.IsStrictVersion(2))
                {
                    base.WriteError(ForEachObjectCommand.GenerateNameParameterError("Property", InternalCommandStrings.PropertyNotFound, "PropertyNotFound", this._inputObject, new object[] { this._property }));
                    error = true;
                }
            }
            else
            {
                try
                {
                    return(matchMembers[0].Value);
                }
                catch (TerminateException)
                {
                    throw;
                }
                catch (MethodException)
                {
                    throw;
                }
                catch (Exception exception)
                {
                    CommandProcessorBase.CheckForSevereException(exception);
                    return(null);
                }
            }
            return(null);
        }
Пример #9
0
        internal List <MshExpression> ResolveNames(PSObject target, bool expand)
        {
            List <MshExpression> list = new List <MshExpression>();

            if (this._isResolved)
            {
                list.Add(this);
                return(list);
            }
            if (this._script != null)
            {
                MshExpression item = new MshExpression(this._script)
                {
                    _isResolved = true
                };
                list.Add(item);
                return(list);
            }
            IEnumerable <PSMemberInfo> enumerable = null;

            if (this.HasWildCardCharacters)
            {
                enumerable = target.Members.Match(this._stringValue, PSMemberTypes.PropertySet | PSMemberTypes.Properties);
            }
            else
            {
                PSMemberInfo        info  = target.Members[this._stringValue];
                List <PSMemberInfo> list2 = new List <PSMemberInfo>();
                if (info != null)
                {
                    list2.Add(info);
                }
                enumerable = list2;
            }
            List <PSMemberInfo> list3 = new List <PSMemberInfo>();

            foreach (PSMemberInfo info2 in enumerable)
            {
                PSPropertySet set = info2 as PSPropertySet;
                if (set != null)
                {
                    if (expand)
                    {
                        Collection <string> referencedPropertyNames = set.ReferencedPropertyNames;
                        for (int i = 0; i < referencedPropertyNames.Count; i++)
                        {
                            ReadOnlyPSMemberInfoCollection <PSPropertyInfo> infos = target.Properties.Match(referencedPropertyNames[i]);
                            for (int j = 0; j < infos.Count; j++)
                            {
                                list3.Add(infos[j]);
                            }
                        }
                    }
                }
                else if (info2 is PSPropertyInfo)
                {
                    list3.Add(info2);
                }
            }
            Hashtable hashtable = new Hashtable();

            foreach (PSMemberInfo info3 in list3)
            {
                if (!hashtable.ContainsKey(info3.Name))
                {
                    MshExpression expression2 = new MshExpression(info3.Name)
                    {
                        _isResolved = true
                    };
                    list.Add(expression2);
                    hashtable.Add(info3.Name, null);
                }
            }
            return(list);
        }
Пример #10
0
        protected override void ProcessRecord()
        {
            string parameterSetName = base.ParameterSetName;

            if (parameterSetName == null)
            {
                return;
            }
            if (!(parameterSetName == "ScriptBlockSet"))
            {
                if (!(parameterSetName == "PropertyAndMethodSet"))
                {
                    return;
                }
            }
            else
            {
                for (int i = this.start; i < this.end; i++)
                {
                    if (this.scripts[i] != null)
                    {
                        this.scripts[i].InvokeUsingCmdlet(this, false, ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe, this.InputObject, new object[] { this.InputObject }, AutomationNull.Value, new object[0]);
                    }
                }
                return;
            }
            this.targetString = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectTarget, new object[] { GetStringRepresentation(this.InputObject) });
            if (LanguagePrimitives.IsNull(this.InputObject))
            {
                if ((this._arguments != null) && (this._arguments.Length > 0))
                {
                    base.WriteError(GenerateNameParameterError("InputObject", ParserStrings.InvokeMethodOnNull, "InvokeMethodOnNull", this._inputObject, new object[0]));
                    return;
                }
                string action = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { this._propertyOrMethodName });
                if (base.ShouldProcess(this.targetString, action))
                {
                    if (base.Context.IsStrictVersion(2))
                    {
                        base.WriteError(GenerateNameParameterError("InputObject", InternalCommandStrings.InputObjectIsNull, "InputObjectIsNull", this._inputObject, new object[0]));
                        return;
                    }
                    base.WriteObject(null);
                }
                return;
            }
            ErrorRecord errorRecord = null;

            if ((this._arguments != null) && (this._arguments.Length > 0))
            {
                this.MethodCallWithArguments();
            }
            else
            {
                if (this.GetValueFromIDictionaryInput())
                {
                    return;
                }
                PSMemberInfo info = null;
                if (WildcardPattern.ContainsWildcardCharacters(this._propertyOrMethodName))
                {
                    ReadOnlyPSMemberInfoCollection <PSMemberInfo> infos = this._inputObject.Members.Match(this._propertyOrMethodName, PSMemberTypes.All);
                    if (infos.Count > 1)
                    {
                        StringBuilder builder = new StringBuilder();
                        foreach (PSMemberInfo info2 in infos)
                        {
                            builder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[] { info2.Name });
                        }
                        base.WriteError(GenerateNameParameterError("Name", InternalCommandStrings.AmbiguousPropertyOrMethodName, "AmbiguousPropertyOrMethodName", this._inputObject, new object[] { this._propertyOrMethodName, builder }));
                        return;
                    }
                    if (infos.Count == 1)
                    {
                        info = infos[0];
                    }
                }
                else
                {
                    info = this._inputObject.Members[this._propertyOrMethodName];
                }
                if (info == null)
                {
                    errorRecord = GenerateNameParameterError("Name", InternalCommandStrings.PropertyOrMethodNotFound, "PropertyOrMethodNotFound", this._inputObject, new object[] { this._propertyOrMethodName });
                }
                else
                {
                    if (info is PSMethodInfo)
                    {
                        PSParameterizedProperty property = info as PSParameterizedProperty;
                        if (property != null)
                        {
                            string str2 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { property.Name });
                            if (base.ShouldProcess(this.targetString, str2))
                            {
                                base.WriteObject(info.Value);
                            }
                            return;
                        }
                        PSMethodInfo info3 = info as PSMethodInfo;
                        try
                        {
                            string str3 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectMethodActionWithoutArguments, new object[] { info3.Name });
                            if (base.ShouldProcess(this.targetString, str3) && !this.BlockMethodInLanguageMode(this.InputObject))
                            {
                                object obj2 = info3.Invoke(new object[0]);
                                this.WriteToPipelineWithUnrolling(obj2);
                            }
                            goto Label_0451;
                        }
                        catch (PipelineStoppedException)
                        {
                            throw;
                        }
                        catch (Exception exception)
                        {
                            CommandProcessorBase.CheckForSevereException(exception);
                            MethodException exception2 = exception as MethodException;
                            if (((exception2 != null) && (exception2.ErrorRecord != null)) && (exception2.ErrorRecord.FullyQualifiedErrorId == "MethodCountCouldNotFindBest"))
                            {
                                base.WriteObject(info3.Value);
                            }
                            else
                            {
                                base.WriteError(new ErrorRecord(exception, "MethodInvocationError", ErrorCategory.InvalidOperation, this._inputObject));
                            }
                            goto Label_0451;
                        }
                    }
                    string str4 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { info.Name });
                    if (base.ShouldProcess(this.targetString, str4))
                    {
                        try
                        {
                            this.WriteToPipelineWithUnrolling(info.Value);
                        }
                        catch (TerminateException)
                        {
                            throw;
                        }
                        catch (MethodException)
                        {
                            throw;
                        }
                        catch (PipelineStoppedException)
                        {
                            throw;
                        }
                        catch (Exception exception3)
                        {
                            CommandProcessorBase.CheckForSevereException(exception3);
                            base.WriteObject(null);
                        }
                    }
                }
            }
Label_0451:
            if (errorRecord != null)
            {
                string str5 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { this._propertyOrMethodName });
                if (base.ShouldProcess(this.targetString, str5))
                {
                    if (base.Context.IsStrictVersion(2))
                    {
                        base.WriteError(errorRecord);
                        return;
                    }
                    base.WriteObject(null);
                }
            }
        }