Пример #1
0
        /// <summary>
        /// Modifies an existing instance based on a custom query.
        /// </summary>
        /// <param name="obj">Object to be updated</param>
        /// <param name="query">Query to be run against WMI. The resulting instances will be updated</param>
        public void UpdateInstance(object obj, string query)
        {
            try
            {
                WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

#if NET45
                WindowsImpersonationContext impersonatedUser = windowsIdentity.Impersonate();
#endif
#if NETSTANDARD20
                WindowsIdentity.RunImpersonated(windowsIdentity.AccessToken, () =>
#endif
                {
                    string className = TypeHelper.GetClassName(obj);

                    ManagementObjectSearcher searcher;
                    searcher = new ManagementObjectSearcher(Scope, new ObjectQuery(query));

                    ManagementObjectCollection col = searcher.Get();

                    foreach (ManagementObject m in searcher.Get())
                    {
                        foreach (PropertyInfo p in obj.GetType().GetProperties())
                        {
                            WMIIgnore ignoreProp = p.GetCustomAttribute<WMIIgnore>();
                            WMIIgnoreOnUpdate ignoreOnUpdateProp = p.GetCustomAttribute<WMIIgnoreOnUpdate>();

                            if (ignoreProp == null && ignoreOnUpdateProp == null)
                            {
                                if (p.GetValue(obj) != null)
                                {
                                    WMIProperty propAtt = p.GetCustomAttribute<WMIProperty>();

                                    if (propAtt != null)
                                    {
                                        m[propAtt.Name] = p.GetValue(obj).GetType() == typeof(DateTime) ? ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(p.GetValue(obj))) : p.GetValue(obj);
                                    }
                                    else
                                    {
                                        m[p.Name] = p.GetValue(obj).GetType() == typeof(DateTime) ? ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(p.GetValue(obj))) : p.GetValue(obj);
                                    }
                                }
                            }
                        }

                        m.Put();
                    }
                }
#if NETSTANDARD20
                );
#endif
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #2
0
        /// <summary>
        /// Modifies an existing instance.
        /// </summary>
        /// <param name="obj">Object to be updated. ORMi will search the property with the SearchKey attribute. That value is going to be used to make the update.</param>
        public void UpdateInstance(object obj)
        {
            try
            {
                WindowsImpersonationContext impersonatedUser = WindowsIdentity.GetCurrent().Impersonate();

                string className = TypeHelper.GetClassName(obj);

                WMISearchKey key = TypeHelper.GetSearchKey(obj);

                if (key.Value != null)
                {
                    string query = String.Format("SELECT * FROM {0} WHERE {1} = '{2}'", TypeHelper.GetClassName(obj), key.Name, key.Value);

                    ManagementObjectSearcher searcher;
                    searcher = new ManagementObjectSearcher(Scope, query);

                    ManagementObjectCollection col = searcher.Get();

                    foreach (ManagementObject m in searcher.Get())
                    {
                        foreach (PropertyInfo p in obj.GetType().GetProperties())
                        {
                            WMIIgnore ignoreProp = p.GetCustomAttribute <WMIIgnore>();

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

                                if (propAtt != null)
                                {
                                    m[propAtt.Name] = p.GetValue(obj).GetType() == typeof(DateTime) ? ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(p.GetValue(obj))) : p.GetValue(obj);
                                }
                                else
                                {
                                    m[p.Name] = p.GetValue(obj).GetType() == typeof(DateTime) ? ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(p.GetValue(obj))) : p.GetValue(obj);
                                }
                            }
                        }

                        m.Put();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #3
0
        /// <summary>
        /// Modifies an existing instance based on a custom query.
        /// </summary>
        /// <param name="obj">Object to be updated</param>
        /// <param name="query">Query to be run against WMI. The resulting instances will be updated</param>
        public void UpdateInstance(object obj, string query)
        {
            try
            {
                WindowsImpersonationContext impersonatedUser = WindowsIdentity.GetCurrent().Impersonate();

                string className = TypeHelper.GetClassName(obj);

                ManagementObjectSearcher searcher;
                searcher = new ManagementObjectSearcher(Scope, query);

                ManagementObjectCollection col = searcher.Get();

                foreach (ManagementObject m in searcher.Get())
                {
                    foreach (PropertyInfo p in obj.GetType().GetProperties())
                    {
                        WMIIgnore ignoreProp = p.GetCustomAttribute <WMIIgnore>();

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

                            if (propAtt != null)
                            {
                                m[propAtt.Name] = p.GetValue(obj);
                            }
                            else
                            {
                                m[p.Name] = p.GetValue(obj);
                            }
                        }
                    }

                    m.Put();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #4
0
        /// <summary>
        /// Modifies an existing instance.
        /// </summary>
        /// <param name="obj">Object to be updated. ORMi will search the property with the SearchKey attribute. That value is going to be used to make the update.</param>
        public void UpdateInstance(object obj)
        {
            try
            {
                WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();

#if NET45
                WindowsImpersonationContext impersonatedUser = windowsIdentity.Impersonate();
#endif
#if NETSTANDARD20
                WindowsIdentity.RunImpersonated(windowsIdentity.AccessToken, () =>
#endif
                {
                    string className = TypeHelper.GetClassName(obj);

                    string query = String.Format("SELECT * FROM {0}", TypeHelper.GetClassName(obj));

                    List<WMISearchKey> keys = TypeHelper.GetSearchKeys(obj);

                    if (keys.Count > 0)
                    {
                        for (int i = 0; i < keys.Count; i++)
                        {
                            if (i == 0)
                            {
                                query = String.Format("{0} WHERE {1} = '{2}'", query, keys[i].Name, keys[i].Value);
                            }
                            else
                            {
                                query = String.Format("{0} AND {1} = '{2}'", query, keys[i].Name, keys[i].Value);
                            }
                        }

                        ManagementObjectSearcher searcher;
                        searcher = new ManagementObjectSearcher(Scope, new ObjectQuery(query));

                        ManagementObjectCollection col = searcher.Get();

                        foreach (ManagementObject m in searcher.Get())
                        {
                            foreach (PropertyInfo p in obj.GetType().GetProperties())
                            {
                                if (p.GetValue(obj) != null)
                                {
                                    WMIIgnore ignoreProp = p.GetCustomAttribute<WMIIgnore>();
                                    WMIIgnoreOnUpdate ignoreOnUpdateProp = p.GetCustomAttribute<WMIIgnoreOnUpdate>();

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

                                        if (propAtt != null)
                                        {
                                            m[propAtt.Name] = p.GetValue(obj).GetType() == typeof(DateTime) ? ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(p.GetValue(obj))) : p.GetValue(obj);
                                        }
                                        else
                                        {
                                            m[p.Name] = p.GetValue(obj).GetType() == typeof(DateTime) ? ManagementDateTimeConverter.ToDmtfDateTime(Convert.ToDateTime(p.GetValue(obj))) : p.GetValue(obj);
                                        }
                                    }
                                }
                            }

                            m.Put();
                        }
                    }
                    else
                    {
                        throw new WMISearchKeyException("There is no SearchKey specified for the object");
                    }
                }
#if NETSTANDARD20
                );
#endif
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }