Example #1
0
        public ILookup <string, WMIKeyValues> GetPrimaryKeyValues <T>()
        {
            var classNmae = HelperFuncs.getClassName(typeof(T));

            var list = new List <WMIKeyValues>();


            using (var wmiObject = new ManagementClass(Scope.Path.NamespacePath, classNmae, options: null))
            {
                foreach (ManagementObject c in wmiObject.GetInstances())
                {
                    foreach (PropertyData p in c.Properties)
                    {
                        foreach (QualifierData q in p.Qualifiers)
                        {
                            if (q.Name.Equals("key"))
                            {
                                list.Add(new WMIKeyValues(p.Name, c.GetPropertyValue(p.Name)));
                            }
                        }
                    }
                }
            }

            ILookup <string, WMIKeyValues> byWMIKey = list.ToLookup(x => x.Key);

            return(byWMIKey);
        }
Example #2
0
        public static string BuildQuery <T>()
        {
            var classNmae = HelperFuncs.getClassName(typeof(T));

            var searchprops = String.Join(" , ", HelperFuncs.getSearchPropsNames(typeof(T)));


            return(String.Format("SELECT {0} FROM {1} ", searchprops, classNmae));
        }
Example #3
0
        /// <summary>
        /// Executes WMI instance method with  parameters
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="classObj"></param>
        /// <param name="parameters">function paramaters</param>
        public static void ExecuteMethod(object classObj, params object[] parameters)
        {
            var inParams = new List <object>();

            foreach (var param in parameters)
            {
                inParams.Add(param);
            }


            var           propsNames = HelperFuncs.getSearchPropsNames(classObj.GetType());
            var           propValues = HelperFuncs.getSearchPropValues(classObj);
            StringBuilder builder    = new StringBuilder();

            var propNamesAndValues = propsNames.Zip(propValues, (pn, pv) => new { propName = pn, propValue = pv }).ToList();


            for (int i = 0; i < propNamesAndValues.Count; i++)
            {
                builder.Append(propNamesAndValues[i].propName + " = '" + propNamesAndValues[i].propValue + "'");
                if (i != propNamesAndValues.Count - 1)
                {
                    builder.Append(" AND ");
                }
            }

            string quertyWhere = "WHERE " + builder.ToString();

            quertyWhere = quertyWhere.Replace(@"\", @"\\");


            var    methodName    = String.IsNullOrEmpty(_methodName) ? new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name : _methodName;
            string searchParams  = "*";
            string rootNamespace = HelperFuncs.getNamespace(classObj.GetType());
            string className     = HelperFuncs.getClassName(classObj.GetType());

            string searchQuery = String.Format("SELECT {0} FROM {1} {2}", searchParams, className, quertyWhere);

            using (var searcher = new ManagementObjectSearcher(rootNamespace, searchQuery))
            {
                using (var searcherData = searcher.Get())
                {
                    foreach (ManagementObject manageObject in searcherData)
                    {
                        manageObject.InvokeMethod(methodName, inParams.ToArray());
                    }
                }
            }
        }
Example #4
0
        public WMIWatcher(string scope, Type type)
        {
            _scope = scope;
            _type  = type;

            string className = HelperFuncs.getClassName(type);


            if (TestIfEventWatchable(scope, className))
            {
                _query = $"SELECT * FROM {className}";
                CreateAndStartWatcher();
            }
            else
            {
                throw new Exception("Class is not an event class.");
            }
        }