Exemple #1
0
        /// <summary>
        /// Creates and caches an instance of the "PSStandardMembers" <see cref="PSMemberSet"/> to attach to a <see cref="Record"/>.
        /// </summary>
        /// <param name="view">The <see cref="View"/> from which column information is retrieved.</param>
        /// <returns>A new or cached copy of the "PSStandardMembers" <see cref="PSMemberSet"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="view"/> parameter value is null.</exception>
        internal static PSMemberSet GetMemberSet(View view)
        {
            if (null == view)
            {
                throw new ArgumentNullException("view");
            }

            PSMemberSet memberSet;

            if (memberSets.ContainsKey(view.QueryString) && memberSets[view.QueryString].IsAlive)
            {
                // Get an existing PSMemberSet.
                memberSet = (PSMemberSet)memberSets[view.QueryString].Target;
            }
            else
            {
                // Add or set a new PSMemberSet.
                memberSet = new PSMemberSet("PSStandardMembers");

                var columns    = ViewManager.GetColumns(view).Select(column => column.Key);
                var properties = new PSPropertySet("DefaultDisplayPropertySet", columns);
                memberSet.Members.Add(properties);

                columns = ViewManager.GetColumns(view).PrimaryKeys;
                if (0 < columns.Count())
                {
                    properties = new PSPropertySet("DefaultKeyPropertySet", columns);
                    memberSet.Members.Add(properties);
                }

                memberSets[view.QueryString] = new WeakReference(memberSet);
            }

            return(memberSet);
        }
    public static PSObject Get()
    {
        var w       = new Whatever();
        var pso     = new PSObject(w);
        var display = new PSPropertySet("DefaultDisplayPropertySet", new [] { "One", "Two" });
        var mi      = new PSMemberSet("PSStandardMembers", new[] { display });

        pso.Members.Add(mi);

        return(pso);
    }
Exemple #3
0
        void WriteResult(ulong sz, string p)
        {
            PSObject o = new PSObject();

            // probably should just calculate that here and insert it as another column
            string human = @"switch($this.Length) { { $_ -gt 1tb } 
                        { ""{ 0:n2}T"" -f ($_ / 1tb) ; break }
                    { $_ -gt 1gb }
                        { ""{0:n2}G"" -f ($_ / 1gb) ; break }
                    { $_ -gt 1mb }
                        { ""{0:n2}M"" -f ($_ / 1mb) ; break }
                    { $_ -gt 1kb }
                        { ""{0:n2}K"" -f ($_ / 1Kb) ; break }
                    default
                        { ""{0}"" -f $_}
                } 
            ";

            string[] props  = { "Length", "FullName" };
            string[] propsh = { "ReadableLength", "FullName" };

            ScriptBlock      sb  = ScriptBlock.Create(human);
            PSScriptProperty psp = new PSScriptProperty("ReadableLength", sb);
            PSPropertySet    ps;

            if (humanreadable)
            {
                ps = new PSPropertySet("DefaultDisplayPropertySet", propsh);
            }
            else
            {
                ps = new PSPropertySet("DefaultDisplayPropertySet", props);
            }

            PSMemberSet pm = new PSMemberSet("PSStandardMembers", new PSMemberInfo[] { ps });

            // pm.Members.Add();

            o.Members.Add(pm);

            o.Properties.Add(new PSNoteProperty("Length", sz));
            o.Properties.Add(new PSNoteProperty("FullName", p));
            o.Members.Add(psp);

            WriteObject(o);
        }
        private static string[] GetDefaultKeyPropertySet(PSObject mshObj)
        {
            PSMemberSet pSStandardMembers = mshObj.PSStandardMembers;

            if (pSStandardMembers == null)
            {
                return(null);
            }
            PSPropertySet set2 = pSStandardMembers.Members["DefaultKeyPropertySet"] as PSPropertySet;

            if (set2 == null)
            {
                return(null);
            }
            string[] array = new string[set2.ReferencedPropertyNames.Count];
            set2.ReferencedPropertyNames.CopyTo(array, 0);
            return(array);
        }
Exemple #5
0
        internal static string[] GetDefaultKeyPropertySet(PSObject mshObj)
        {
            PSMemberSet standardNames = mshObj.PSStandardMembers;

            if (standardNames == null)
            {
                return(null);
            }
            PSPropertySet defaultKeys = standardNames.Members["DefaultKeyPropertySet"] as PSPropertySet;

            if (defaultKeys == null)
            {
                return(null);
            }
            string[] props = new string[defaultKeys.ReferencedPropertyNames.Count];
            defaultKeys.ReferencedPropertyNames.CopyTo(props, 0);
            return(props);
        }
Exemple #6
0
 private static List <MshExpression> GetDefaultPropertySet(PSMemberSet standardMembersSet)
 {
     if (standardMembersSet != null)
     {
         PSPropertySet set = standardMembersSet.Members["DefaultDisplayPropertySet"] as PSPropertySet;
         if (set != null)
         {
             List <MshExpression> list = new List <MshExpression>();
             foreach (string str in set.ReferencedPropertyNames)
             {
                 if (!string.IsNullOrEmpty(str))
                 {
                     list.Add(new MshExpression(str));
                 }
             }
             return(list);
         }
     }
     return(new List <MshExpression>());
 }
        private static List <PSPropertyExpression> GetDefaultPropertySet(PSMemberSet standardMembersSet)
        {
            if (standardMembersSet != null)
            {
                PSPropertySet defaultDisplayPropertySet = standardMembersSet.Members[TypeTable.DefaultDisplayPropertySet] as PSPropertySet;
                if (defaultDisplayPropertySet != null)
                {
                    List <PSPropertyExpression> retVal = new List <PSPropertyExpression>();
                    foreach (string prop in defaultDisplayPropertySet.ReferencedPropertyNames)
                    {
                        if (!string.IsNullOrEmpty(prop))
                        {
                            retVal.Add(new PSPropertyExpression(prop));
                        }
                    }
                    return(retVal);
                }
            }

            return(new List <PSPropertyExpression>());
        }
Exemple #8
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);
        }
Exemple #9
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);
        }
Exemple #10
0
        /// <summary>
        /// Gets property records from the item.
        /// </summary>
        /// <param name="item">The item from which properties are retrieved.</param>
        protected override void ProcessItem(PSObject item)
        {
            var path         = item.GetPropertyValue <string>("PSPath");
            var providerPath = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(path);

            var db = this.OpenDatabase(providerPath);

            if (null != db)
            {
                // Keep track of which properties were selected.
                IList <string> properties = null;
                if (this.PassThru)
                {
                    properties = new List <string>();
                }

                using (db)
                {
                    TransformView transform = null;
                    if (db.Tables.Contains(TransformView.TableName))
                    {
                        transform = new TransformView(db);
                    }

                    var table = "Property";
                    if ((db is PatchPackage) && db.Tables.Contains("MsiPatchMetadata"))
                    {
                        table = "MsiPatchMetadata";
                    }

                    var query = string.Format("SELECT `Property`, `Value` FROM `{0}`", table);
                    using (var view = db.OpenView(query))
                    {
                        view.Execute();

                        // Get column information from the view before being disposed.
                        var columns = ViewManager.GetColumns(view);

                        var record = view.Fetch();
                        while (null != record)
                        {
                            using (record)
                            {
                                var name = record.GetString(1);
                                if (0 == this.propertyPatterns.Count() || name.Match(this.propertyPatterns))
                                {
                                    if (this.PassThru)
                                    {
                                        if (0 == item.Properties.Match(name).Count())
                                        {
                                            properties.Add(name);

                                            var property = new PSNoteProperty(name, record.GetString(2));
                                            item.Properties.Add(property, true);
                                        }
                                    }
                                    else
                                    {
                                        // Create a locally cached copy of the record.
                                        var copy = new Record(record, columns, transform, providerPath);
                                        var obj  = PSObject.AsPSObject(copy);

                                        // Show only column properties by default.
                                        var memberSet = ViewManager.GetMemberSet(view);
                                        obj.Members.Add(memberSet, true);

                                        base.WriteObject(obj);
                                    }
                                }
                            }

                            record = view.Fetch();
                        }
                    }
                }

                if (this.PassThru)
                {
                    var propertySet = new PSPropertySet("MSIProperties", properties);
                    item.Members.Add(propertySet, true);

                    base.WriteObject(item);
                }
            }
        }
Exemple #11
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);
        }
Exemple #12
0
        public static PSObject EIDResult(EIDResult objectToDecorate, List <PSNoteProperty> fields, List <string> defaults)
        {
            // List of properties to add and defaults to set
            if (fields is null)
            {
                fields = new List <PSNoteProperty>();
            }

            if (defaults is null)
            {
                defaults = new List <string>();
            }

            //Create the object
            PSObject resultObject = new PSObject(objectToDecorate);

            resultObject.Members.Add(new PSNoteProperty("Status", objectToDecorate.Status));

            //Setup response specific display properties
            switch (objectToDecorate.Status)
            {
            case eidprovider.EIDResult.ResultStatus.initialized:
                fields.Add(new PSNoteProperty("Id", objectToDecorate["id"].ToString()));
                fields.Add(new PSNoteProperty("Code", "initialized"));
                fields.Add(new PSNoteProperty("AutostartToken", objectToDecorate["extra"]["autostart_token"].ToString()));
                fields.Add(new PSNoteProperty("AutostartUrl", objectToDecorate["extra"]["autostart_url"].ToString()));
                defaults.InsertRange(0, new string[] { "Status", "Code", "Id" });
                break;

            case eidprovider.EIDResult.ResultStatus.error:
                fields.Add(new PSNoteProperty("Code", objectToDecorate["code"].ToString()));
                fields.Add(new PSNoteProperty("Description", objectToDecorate["description"].ToString()));
                fields.Add(new PSNoteProperty("Details", KeyExist(objectToDecorate, "details") ? objectToDecorate["details"].ToString() : string.Empty));
                defaults.InsertRange(0, new string[] { "Status", "Code" });
                break;

            case eidprovider.EIDResult.ResultStatus.pending:
                fields.Add(new PSNoteProperty("Code", objectToDecorate["code"].ToString()));
                fields.Add(new PSNoteProperty("Description", objectToDecorate["description"].ToString()));
                defaults.InsertRange(0, new string[] { "Status", "Code" });
                break;

            case eidprovider.EIDResult.ResultStatus.ok:
                fields.Add(new PSNoteProperty("Code", objectToDecorate["code"].ToString()));
                fields.Add(new PSNoteProperty("Description", objectToDecorate["description"].ToString()));
                defaults.InsertRange(0, new string[] { "Status", "Code" });

                PSObject okExtraObject = new PSObject();
                if (KeyExist(objectToDecorate, "extra"))
                {
                    ((JObject)objectToDecorate["extra"]).Properties().Select(p => p.Name).ToList().ForEach((item) => { okExtraObject.Members.Add(new PSNoteProperty(item, objectToDecorate["extra"][item].ToString())); });
                }
                fields.Add(new PSNoteProperty("Extra", okExtraObject));
                break;

            case eidprovider.EIDResult.ResultStatus.completed:
                fields.Add(new PSNoteProperty("Id", objectToDecorate["user"]["id"].ToString()));
                fields.Add(new PSNoteProperty("Code", "completed"));
                fields.Add(new PSNoteProperty("Firstname", objectToDecorate["user"]["firstname"].ToString()));
                fields.Add(new PSNoteProperty("Lastname", objectToDecorate["user"]["lastname"].ToString()));
                fields.Add(new PSNoteProperty("Fullname", objectToDecorate["user"]["fullname"].ToString()));

                PSObject psCustomObject = new PSObject();
                if (KeyExist(objectToDecorate, "extra"))
                {
                    ((JObject)objectToDecorate["extra"]).Properties().Select(p => p.Name).ToList().ForEach((item) => { psCustomObject.Members.Add(new PSNoteProperty(item, objectToDecorate["extra"][item].ToString())); });
                }
                fields.Add(new PSNoteProperty("Extra", psCustomObject));

                defaults.InsertRange(0, new string[] { "Status", "Id", "Firstname", "Lastname", "Fullname" });
                break;

            default:
                defaults.InsertRange(0, new string[] { "Status" });
                break;
            }

            // Add the new properties to the real object
            fields.ForEach((p) => { resultObject.Members.Add(p); });

            //Add default display designator
            PSPropertySet defaltDisplayProperties = new PSPropertySet("DefaultDisplayPropertySet", defaults.ToArray());

            resultObject.Members.Add(new PSMemberSet("PSStandardMembers", new[] { defaltDisplayProperties }));

            return(resultObject);
        }