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); } } }
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); }
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)); }
/// <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; } }
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); }
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); }
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); } } } }
/// <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; } }
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); }
/// <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; } }
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); } } } }
/// <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; } }
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); } } } }
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); } } } }