コード例 #1
0
ファイル: WmiHandler.cs プロジェクト: DenisLjubarets/NetScan
        /// <summary>
        /// Query host with given list of <see cref="WmiQuery"/> and fire <see cref="OnHostDataReceived(HostDataReceivedEventArgs)"/>
        /// for each received <see cref="WmiParameter"/>
        /// </summary>
        /// <param name="host">Host to query</param>
        /// <param name="credential">Administrator credential that is able to run WMI requests</param>
        /// <param name="wmiQueries">List of WMI queries to run</param>
        public void ScanHostAsync(IPAddress host, NetworkCredential credential, List <WmiQuery> wmiQueries)
        {
            // Enable encryption for WMI packets for security
            var options = new WmiConnectionOptions {
                EnablePackageEncryption = true
            };

            // Initialize new WMI connection
            using (var connection = new WmiConnection($"\\\\{host}\\root\\cimv2", credential, options))
            {
                // For each query in list of queries
                Parallel.ForEach(wmiQueries, wmiQuery =>
                {
                    try
                    {
                        // This foreach loop runs once, creates and executes WMI query and stores result of it in wmiObject
                        foreach (var wmiObject in connection.CreateQuery($"SELECT * FROM {wmiQuery.Class}"))
                        {
                            // Gets all available properties in WmiObject for current WMI class
                            var availableProperties = new List <string>(wmiObject.GetPropertyNames());

                            // For each property in given WMI query
                            foreach (var property in wmiQuery.Properties)
                            {
                                // If current class contains requested property then get it's value, if not - send message that it was not found
                                var value = availableProperties.Contains(property) ? wmiObject.GetPropertyValue <string>(property) : $"{property} query not found";

                                // Prepare OnHostDataReceived and fire it
                                ExecuteOnHostDataReceived(host, property, value);
                            }
                        }
                    }
                    catch (Exception exception)
                    {
                        // Prepare OnHostDataReceived and fire it with NULL parameter and exception message as a value
                        ExecuteOnHostDataReceived(host, "NULL", exception.Message.Split('.')[0]);
                        return;
                    }
                });
            }

            // This host was completely scanned
            OnHostScanComplete();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: xiaohszx/WmiLight
        static void Main(string[] args)
        {
            Console.WriteLine("Win32_Process:");
            Console.WriteLine("------------------------");

            var opt = new WmiConnectionOptions()
            {
                EnablePackageEncryption = true
            };
            var cred = new NetworkCredential("USERNAME", "PASSWORD", "DOMAIN");

            using (WmiConnection conncetion = new WmiConnection(@"\\MACHINENAME\root\cimv2", cred, opt))
            {
                foreach (WmiObject partition in conncetion.CreateQuery("SELECT * FROM Win32_DiskPartition"))
                {
                    Console.WriteLine(partition["Name"]);
                }
            }

            Console.WriteLine("");

            Console.Write("Press Any Key To Exit...");
            Console.ReadKey();
        }