예제 #1
0
파일: WMISearcher.cs 프로젝트: Styxer/Kexla
        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);
        }
예제 #2
0
        private void Watcher_EventArrived(object sender, EventArrivedEventArgs e)
        {
            object obj = HelperFuncs.getSearchObjects(e.NewEvent, _type);

            WMIEventArrived(this, new WMIEventArgs {
                Object = obj
            });
        }
예제 #3
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));
        }
예제 #4
0
파일: WMIMethods.cs 프로젝트: Styxer/Kexla
        /// <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());
                    }
                }
            }
        }
예제 #5
0
파일: WMISearcher.cs 프로젝트: Styxer/Kexla
        /// <summary>
        /// Runs a query against WMI. It will return all instances of the class corresponding to the WMI class set on the Type on IEnumerable.
        /// </summary>
        /// <typeparam name="T">The Type of IEnumerable that will be returned</typeparam>
        /// <returns></returns>
        public IEnumerable <T> Query <T>(string searchQuery = "")
        {
            searchQuery = String.IsNullOrEmpty(searchQuery) ? HelperFuncs.BuildQuery <T>() : searchQuery;


            using (var searcher = new ManagementObjectSearcher(Scope, new ObjectQuery(searchQuery)))
            {
                using (var searcherData = searcher.Get())
                {
                    foreach (ManagementObject obj in searcherData)
                    {
                        yield return((T)HelperFuncs.getSearchObjects(obj, typeof(T)));
                    }
                }
            }
        }
예제 #6
0
파일: WMIHelper.cs 프로젝트: Styxer/Kexla
        public static string toString(object obj)
        {
            var sb = new StringBuilder();

            var propsNames = HelperFuncs.getSearchPropsNames(obj.GetType());
            var propValues = HelperFuncs.getSearchPropValues(obj);

            var propNamesAndValues = propsNames.Zip(propValues, (pName, pValue) => new { propName = pName, propValue = pValue });

            foreach (var item in propNamesAndValues)
            {
                sb.AppendLine(item.propName + ": " + item.propValue);
            }

            return(sb.ToString());
        }
예제 #7
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.");
            }
        }