Ejemplo n.º 1
0
        private static void _SetPropertyValue(ManagementBaseObject mo, PropertyInfo p, object o)
        {
            WMIIgnore ignoreProp = p.GetCustomAttribute <WMIIgnore>();

            if (ignoreProp == null)
            {
                WMIProperty propAtt = p.GetCustomAttribute <WMIProperty>();

                string propertyName = String.Empty;

                if (propAtt != null)
                {
                    propertyName = propAtt.Name;
                }
                else
                {
                    propertyName = p.Name;
                }

                var a = mo.Properties[propertyName].Value;

                if (a == null)
                {
                    p.SetValue(o, null);
                }
                else if (p.PropertyType == typeof(DateTime) && a is string s)
                {
                    p.SetValue(o, ManagementDateTimeConverter.ToDateTime((string)a), null);
                }
                else
                {
                    p.SetValue(o, Convert.ChangeType(a, p.PropertyType), null);
                }
            }
        }
Ejemplo n.º 2
0
        public static WMISearchKey GetSearchKey(object p)
        {
            WMISearchKey res = null;

            foreach (PropertyInfo propertyInfo in p.GetType().GetProperties())
            {
                WMIIgnore ignoreProp = propertyInfo.GetCustomAttribute <WMIIgnore>();

                if (ignoreProp == null)
                {
                    WMIProperty propAtt = propertyInfo.GetCustomAttribute <WMIProperty>();

                    if (propAtt != null)
                    {
                        if (propAtt.SearchKey)
                        {
                            res = new WMISearchKey
                            {
                                Name  = propAtt.Name,
                                Value = propertyInfo.GetValue(p)
                            };

                            break;
                        }
                    }
                }
            }

            return(res);
        }
Ejemplo n.º 3
0
        public static string GetPropertiesToSearch(Type type)
        {
            List <String> res = new List <string>();

            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                WMIIgnore ignoreProp = propertyInfo.GetCustomAttribute <WMIIgnore>();

                if (ignoreProp == null)
                {
                    WMIProperty propAtt = propertyInfo.GetCustomAttribute <WMIProperty>();

                    if (propAtt == null)
                    {
                        res.Add(propertyInfo.Name.ToUpper());
                    }
                    else
                    {
                        res.Add(propAtt.Name.ToUpper());
                    }
                }
            }

            return(String.Join(",", res));
        }
Ejemplo n.º 4
0
        public static ManagementObject GetManagementObject(ManagementClass sourceClass, object obj)
        {
            var caller = new StackTrace().GetFrame(1).GetMethod().DeclaringType.Name;

            ManagementObject genericInstance;

            try
            {
                genericInstance = sourceClass.CreateInstance();
            }
            catch (ManagementException ex) when(ex.ErrorCode == ManagementStatus.NotFound)
            {
                // rethrow with actual class name we tried
                throw new Exception($"Couldn't find management class {sourceClass}", ex);
            }

            foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
            {
                if (propertyInfo.GetValue(obj) != null)
                {
                    WMIIgnore         ignoreProp     = propertyInfo.GetCustomAttribute <WMIIgnore>();
                    WMIIgnoreOnInsert ignoreOnInsert = propertyInfo.GetCustomAttribute <WMIIgnoreOnInsert>();

                    if (ignoreProp == null && ((caller == "WMIHelper" ? (ignoreOnInsert == null ? true : false) : true)))
                    {
                        WMIProperty propAtt = propertyInfo.GetCustomAttribute <WMIProperty>();

                        if (propAtt == null)
                        {
                            if (propertyInfo.GetValue(obj).GetType() == typeof(DateTime))
                            {
                                genericInstance[propertyInfo.Name.ToUpper()] = ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(propertyInfo.GetValue(obj)));
                            }
                            else
                            {
                                genericInstance[propertyInfo.Name.ToUpper()] = propertyInfo.GetValue(obj);
                            }
                        }
                        else
                        {
                            if (propertyInfo.GetValue(obj).GetType() == typeof(DateTime))
                            {
                                genericInstance[propAtt.Name.ToUpper()] = ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(propertyInfo.GetValue(obj)));
                            }
                            else
                            {
                                genericInstance[propAtt.Name.ToUpper()] = propertyInfo.GetValue(obj);
                            }
                        }
                    }
                }
            }

            return(genericInstance);
        }
Ejemplo n.º 5
0
        public static List <SearchKey> GetSearchKeys(object p)
        {
            List <SearchKey> res = new List <SearchKey>();

            foreach (PropertyInfo propertyInfo in p.GetType().GetProperties())
            {
                WMIIgnore ignoreProp = propertyInfo.GetCustomAttribute <WMIIgnore>();

                if (ignoreProp == null)
                {
                    WMISearchKey searchAttribute = propertyInfo.GetCustomAttribute <WMISearchKey>();

                    if (searchAttribute != null)
                    {
                        WMIProperty propAtt = propertyInfo.GetCustomAttribute <WMIProperty>();

                        if (propAtt != null)
                        {
                            res.Add(new SearchKey
                            {
                                Name  = propAtt.Name,
                                Value = propertyInfo.GetValue(p)
                            });
                        }
                        else
                        {
                            res.Add(new SearchKey
                            {
                                Name  = propertyInfo.Name,
                                Value = propertyInfo.GetValue(p)
                            });
                        }
                    }
                    else
                    {
                        WMIProperty propAtt = propertyInfo.GetCustomAttribute <WMIProperty>();

                        if (propAtt != null)
                        {
                            if (propAtt.SearchKey)
                            {
                                res.Add(new SearchKey
                                {
                                    Name  = propAtt.Name,
                                    Value = propertyInfo.GetValue(p)
                                });
                            }
                        }
                    }
                }
            }

            return(res);
        }
Ejemplo n.º 6
0
        private static void _SetPropertyValue(ManagementBaseObject mo, PropertyInfo p, object o)
        {
            WMIIgnore ignoreProp = p.GetCustomAttribute <WMIIgnore>();

            if (ignoreProp == null)
            {
                WMIProperty propAtt = p.GetCustomAttribute <WMIProperty>();

                string propertyName = String.Empty;

                if (propAtt != null)
                {
                    propertyName = propAtt.Name;
                }
                else
                {
                    propertyName = p.Name;
                }

                var a = mo.Properties[propertyName].Value;

                if (a == null)
                {
                    p.SetValue(o, null);
                }
                else if (p.PropertyType == typeof(DateTime) && a is string s)
                {
                    p.SetValue(o, ManagementDateTimeConverter.ToDateTime((string)a), null);
                }
                else
                {
                    var propertyType = p.PropertyType;
                    if (propertyType.IsGenericType &&
                        propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        propertyType = propertyType.GetGenericArguments()[0];
                    }
                    p.SetValue(o, Convert.ChangeType(a, propertyType), null);
                }
            }
            else
            {
                if (o.GetType().BaseType == typeof(WMIInstance))
                {
                    if (p.Name == "Scope")
                    {
                        p.SetValue(o, ((ManagementObject)(mo)).Scope);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public static ManagementObject GetManagementObject(ManagementClass sourceClass, object obj)
        {
            ManagementObject genericInstance;

            try
            {
                genericInstance = sourceClass.CreateInstance();
            }
            catch (ManagementException ex) when(ex.ErrorCode == ManagementStatus.NotFound)
            {
                // rethrow with actual class name we tried
                throw new Exception($"Couldn't find management class {sourceClass}", ex);
            }

            foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties())
            {
                WMIIgnore ignoreProp = propertyInfo.GetCustomAttribute <WMIIgnore>();

                if (ignoreProp == null)
                {
                    WMIProperty propAtt = propertyInfo.GetCustomAttribute <WMIProperty>();

                    if (propAtt == null)
                    {
                        if (propertyInfo.PropertyType == typeof(DateTime))
                        {
                            genericInstance[propertyInfo.Name.ToUpper()] = ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(propertyInfo.GetValue(obj)));
                        }
                        else
                        {
                            genericInstance[propertyInfo.Name.ToUpper()] = propertyInfo.GetValue(obj);
                        }
                    }
                    else
                    {
                        if (propertyInfo.PropertyType == typeof(DateTime))
                        {
                            genericInstance[propAtt.Name.ToUpper()] = ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(propertyInfo.GetValue(obj)));
                        }
                        else
                        {
                            genericInstance[propAtt.Name.ToUpper()] = propertyInfo.GetValue(obj);
                        }
                    }
                }
            }

            return(genericInstance);
        }
Ejemplo n.º 8
0
        private static void _SetPropertyValue(ManagementBaseObject mo, PropertyInfo p, object o, ManagementScope fallbackScope = null)
        {
            WMIIgnore ignoreProp = p.GetCustomAttribute <WMIIgnore>();

            if (ignoreProp == null)
            {
                WMIProperty propAtt = p.GetCustomAttribute <WMIProperty>();

                string propertyName = String.Empty;

                if (propAtt != null)
                {
                    propertyName = propAtt.Name;
                }
                else
                {
                    propertyName = p.Name;
                }

                var a = mo.Properties[propertyName].Value;

                if (a == null)
                {
                    p.SetValue(o, null);
                }
                else if (p.PropertyType == typeof(DateTime) && a is string s)
                {
                    p.SetValue(o, ManagementDateTimeConverter.ToDateTime((string)a), null);
                }
                else if (p.PropertyType.IsEnum && Enum.IsDefined(p.PropertyType, a))
                {
                    p.SetValue(o, Enum.ToObject(p.PropertyType, a));
                }
                else if (typeof(WMIInstance).IsAssignableFrom(p.PropertyType) && a is ManagementBaseObject nestedMo)
                {
                    // If the property is another WMI instance type, load it
                    p.SetValue(o, LoadObject(nestedMo, p.PropertyType, fallbackScope));
                }
                else
                {
                    var propertyType = p.PropertyType;
                    if (propertyType.IsGenericType &&
                        propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        propertyType = propertyType.GetGenericArguments()[0];
                    }
                    p.SetValue(o, Convert.ChangeType(a, propertyType), null);
                }
            }
            else
            {
                if (o.GetType().BaseType == typeof(WMIInstance) && p.Name == nameof(WMIInstance.Scope))
                {
                    // If we have a ManagementObject with a scope, use that
                    if (mo is ManagementObject smo)
                    {
                        p.SetValue(o, smo.Scope);
                    }
                    // Otherwise, use a fallback scope if provided
                    else if (fallbackScope != null)
                    {
                        p.SetValue(o, fallbackScope);
                    }
                }
            }
        }
Ejemplo n.º 9
0
        private static void _SetPropertyValue(ManagementBaseObject mo, PropertyInfo p, object o)
        {
            WMIIgnore ignoreProp = p.GetCustomAttribute <WMIIgnore>();

            if (ignoreProp == null)
            {
                try
                {
                    WMIProperty propAtt = p.GetCustomAttribute <WMIProperty>();

                    string propertyName = String.Empty;

                    if (propAtt != null)
                    {
                        propertyName = propAtt.Name;
                    }
                    else
                    {
                        propertyName = p.Name;
                    }

                    var a = mo.Properties[propertyName].Value;

                    if (a == null)
                    {
                        p.SetValue(o, null);
                    }
                    else if (p.PropertyType == typeof(DateTime) && a is string s)
                    {
                        p.SetValue(o, ManagementDateTimeConverter.ToDateTime((string)a), null);
                    }
                    else if (a is ManagementBaseObject b)
                    {
                        var classAtt = p.PropertyType.GetCustomAttribute <WMIClass>();

                        string className = String.Empty;

                        if (classAtt != null)
                        {
                            className = classAtt.Name;
                        }
                        else
                        {
                            className = p.PropertyType.Name;
                        }

                        if (className == b.ClassPath.ClassName)
                        {
                            p.SetValue(o, LoadObject(b, p.PropertyType), null);
                        }
                    }
                    else
                    {
                        var propertyType = p.PropertyType;
                        if (propertyType.IsGenericType &&
                            propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            propertyType = propertyType.GetGenericArguments()[0];
                        }
                        p.SetValue(o, Convert.ChangeType(a, propertyType), null);
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception($"Property name was not found on WMI object. Check out {o} property names and attributes");
                }
            }
            else
            {
                if (o.GetType().BaseType == typeof(WMIInstance))
                {
                    if (p.Name == "Scope")
                    {
                        p.SetValue(o, ((ManagementObject)(mo)).Scope);
                    }
                }
            }
        }
Ejemplo n.º 10
0
        private static void _SetPropertyValue(ManagementBaseObject mo, PropertyInfo p, object o)
        {
            WMIIgnore ignoreProp = p.GetCustomAttribute <WMIIgnore>();

            if (ignoreProp == null)
            {
                WMIProperty propAtt = p.GetCustomAttribute <WMIProperty>();

                string propertyName = String.Empty;

                if (propAtt != null)
                {
                    propertyName = propAtt.Name;
                }
                else
                {
                    propertyName = p.Name;
                }

                object a;
                try
                {
                    a = mo.Properties[propertyName].Value;
                }
                catch (ManagementException me)
                {
                    if (me.ErrorCode == ManagementStatus.NotFound)
                    {
                        throw new Exception($"Could not find property '{propertyName}' in the response", me);
                    }
                    else
                    {
                        throw;
                    }
                }

                if (a == null)
                {
                    p.SetValue(o, null);
                }
                else if (p.PropertyType == typeof(DateTime) && a is string s)
                {
                    p.SetValue(o, ManagementDateTimeConverter.ToDateTime((string)a), null);
                }
                else
                {
                    var propertyType = p.PropertyType;
                    if (propertyType.IsGenericType &&
                        propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        propertyType = propertyType.GetGenericArguments()[0];
                    }
                    p.SetValue(o, Convert.ChangeType(a, propertyType), null);
                }
            }
            else
            {
                if (o.GetType().BaseType == typeof(WMIInstance))
                {
                    if (p.Name == "Scope")
                    {
                        p.SetValue(o, ((ManagementObject)(mo)).Scope);
                    }
                }
            }
        }