public int GetHDDCount() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return(context.Source <Win32_LogicalDisk>().Where(d => d.DriveType == 3).Count()); } catch { return(0); } }
public string GetBIOSVersion() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return(context.Source <Win32_BIOS>().First().SMBIOSBIOSVersion); } catch { return(string.Empty); } }
public string GetMoboBrand() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return(context.Source <Win32_BaseBoard>().First().Manufacturer); } catch { return(string.Empty); } }
public string GetMoboModel() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return(context.Source <Win32_BaseBoard>().First().Product); } catch { return(string.Empty); } }
public string GetSoundcardName() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return(context.Source <Win32_SoundDevice>().First().Name); } catch { return(string.Empty); } }
/// <summary> /// Sets WMI context instance for WMI Query provider /// </summary> /// <param name="context">WMIContext instance</param> public void SetContext(WmiContext context) { if (_context != null) { throw new WmiContextReassignmentException(nameof(WmiContext)); } _context = context ?? throw new ArgumentNullException(nameof(WmiContext)); }
public int GetCPUBits() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return((int)context.Source <Win32_Processor>().First().AddressWidth); } catch { return(1); } }
private static void QueryAccounts(WmiContext context) { var query = from account in context.Source<Win32_UserAccount>() select account; foreach (Win32_UserAccount account in query) { Console.WriteLine(account.Name); } }
private static void QueryAccounts(WmiContext context) { var query = from account in context.Source <Win32_UserAccount>() select account; foreach (Win32_UserAccount account in query) { Console.WriteLine(account.Name); } }
public int GetGPUCount() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { return(context.Source <Win32_VideoController>().Count()); } } catch { return(1); } }
/// <summary> /// Example of querying CPUs /// </summary> /// <param name="context">The WMI query context</param> private static void QueryProcessors(WmiContext context) { var query = from processor in context.Source <Win32_Processor>() select processor; foreach (Win32_Processor processor in query) { Console.WriteLine(String.Format("DeviceID: {0}", processor.DeviceID)); Console.WriteLine(String.Format("Usage: {0}%", processor.LoadPercentage)); } }
public string GetGPUDescription() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { return(context.Source <Win32_VideoController>().First().Name); } } catch { return(string.Empty); } }
/// <summary> /// Queries the available system memory /// </summary> /// <param name="context"></param> private static void QueryAvailableMemory(WmiContext context) { var query = from memory in context.Source <Win32_PerfFormattedData_PerfOS_Memory>() select memory; Console.WriteLine(String.Format("Available memory: {0}MB.", query.Single().AvailableMBytes)); ulong availMemory = (from computerSystem in context.Source <Win32_ComputerSystem>() select computerSystem.TotalPhysicalMemory / 1048576).Single(); Console.WriteLine(String.Format("Available memory: {0}MB.", availMemory)); }
public long GetTotalGPUMemory() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { var totals = from card in context.Source <Win32_VideoController>() select(long) card.AdapterRAM; return(totals.Sum()); } } catch { return(1); } }
public long GetMemorySize() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { var totals = from stick in context.Source <Win32_PhysicalMemory>() select(long) stick.Capacity; return(totals.Sum()); } } catch { return(0); } }
static void Main(string[] args) { using (WmiContext context = new WmiContext(@"\\.")) { context.ManagementScope.Options.Impersonation = ImpersonationLevel.Impersonate; context.Log = Console.Out; Test(context); } Thread.Sleep(10000); }
public int GetCPUCores() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { return (int)context.Source<Win32_Processor>().First().NumberOfCores; } } catch { return 1; } }
/// <summary> /// Example of querying the eventlog 'Application' and take 5 /// </summary> /// <param name="context"></param> public static void QueryEvents(WmiContext context) { //NOTE: We havent overloaded 'Take' so it deferres to the not optimized IEnumerable<T> of Take var query = (from evt in context.Source <Win32_NTLogEvent>() where evt.Logfile == "Application" && evt.Message.Contains(".exe") select evt).Take(5); foreach (Win32_NTLogEvent evt in query) { Console.WriteLine(evt.TimeWritten); Console.WriteLine(evt.Message); } }
/// <summary> /// Example of querying system drives /// Query drives that do not have an X in their name, and more that 1024 bytes of space /// </summary> public static void QueryDrives(WmiContext context) { var query = from drive in context.Source<Win32_LogicalDisk>() where drive.DriveType == 3 || drive.DriveType == 4 select drive; foreach (Win32_LogicalDisk drive in query) { Console.WriteLine(String.Format("Drive {0} has {1}MB free. Total size: {2}MB.", drive.DeviceID, drive.FreeSpace / 106496, drive.Size / 106496, drive.DriveType)); } }
/// <summary> /// Example of querying the eventlog 'Application' and take 5 /// </summary> /// <param name="context"></param> public static void QueryEvents(WmiContext context) { //NOTE: We havent overloaded 'Take' so it deferres to the not optimized IEnumerable<T> of Take var query = (from evt in context.Source<Win32_NTLogEvent>() where evt.Logfile == "Application" && evt.Message.Contains(".exe") select evt).Take(5); foreach (Win32_NTLogEvent evt in query) { Console.WriteLine(evt.TimeWritten); Console.WriteLine(evt.Message); } }
public int GetCPUThreads() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { return((int)context.Source <Win32_Processor>().First().NumberOfLogicalProcessors); } } catch { return(1); } }
public long GetTotalHDDFree() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { var totals = from disk in context.Source <Win32_LogicalDisk>() where disk.DriveType == 3 select(long) disk.FreeSpace; return(totals.Sum()); } } catch { return(0); } }
/// <summary> /// Example of querying system drives /// Query drives that do not have an X in their name, and more that 1024 bytes of space /// </summary> public static void QueryDrives(WmiContext context) { var query = from drive in context.Source <Win32_LogicalDisk>() where drive.DriveType == 3 || drive.DriveType == 4 select drive; foreach (Win32_LogicalDisk drive in query) { Console.WriteLine(String.Format("Drive {0} has {1}MB free. Total size: {2}MB.", drive.DeviceID, drive.FreeSpace / 106496, drive.Size / 106496, drive.DriveType)); } }
static void Main(string[] args) { using (WmiContext context = new WmiContext(@"\\.")) { context.ManagementScope.Options.Impersonation = ImpersonationLevel.Impersonate; context.Log = Console.Out; QueryEvents(context); QueryProcessors(context); QueryDrives(context); QueryAvailableMemory(context); QueryAccounts(context); } Thread.Sleep(3000); }
public WmiQueryBuilder(WmiContext context) { RegisterWhereVisitors(); }
public override CollectedData OnAcquire() { bool success = true; List <HardDisk> disks = new List <HardDisk>(); CollectedData cd = new CollectedData(Context, success); OnAcquireDelegate( dict => { string serial_num = string.Empty; if (dict.TryGetValue("SerialNumber", out object o)) { serial_num = o.ToString().Trim(); } if (string.IsNullOrEmpty(serial_num) == false) { HardDisk d = new HardDisk() { DeviceID = dict["DeviceID"]?.ToString().Trim(), Model = dict["Model"]?.ToString().Trim(), PnpDeviceID = dict["PNPDeviceID"]?.ToString().Trim(), InterfaceType = dict["InterfaceType"]?.ToString().Trim(), SerialNum = serial_num }; disks.Add(d); } }); if (disks.Count > 0) { ListData <HardDisk> disks2 = new ListData <HardDisk>(Context); foreach (HardDisk disk in disks) { try { // Figure out which drive letters are on this hard disk ManagementScope scope = WmiContext.GetManagementScope(); string drive_pnp = disk.PnpDeviceID.Replace("\\", "\\\\"); string queryStr = string.Format("ASSOCIATORS OF {{Win32_DiskDrive.DeviceID='{0}'}} WHERE AssocClass = Win32_DiskDriveToDiskPartition", disk.DeviceID); //Console.WriteLine(queryStr); foreach (ManagementBaseObject partition in new ManagementObjectSearcher(scope, new ObjectQuery(queryStr)).Get()) { queryStr = string.Format("ASSOCIATORS OF {{Win32_DiskPartition.DeviceID='{0}'}} WHERE AssocClass = Win32_LogicalDiskToPartition", partition["DeviceID"]); //Console.WriteLine(queryStr); foreach (ManagementBaseObject o in new ManagementObjectSearcher(scope, new ObjectQuery(queryStr)).Get()) { string drive_letter = o["Name"].ToString().Trim().ToUpper(); if (disk.DriveLetters.Contains(drive_letter) == false) { disk.DriveLetters.Add(drive_letter); } } } // See if the drive is predicting failure scope = WmiContext.GetManagementScope("WMI"); queryStr = string.Format("SELECT * FROM MSStorageDriver_FailurePredictStatus WHERE InstanceName LIKE \"%{0}%\"", drive_pnp); //Console.WriteLine(queryStr); foreach (ManagementBaseObject m in new ManagementObjectSearcher(scope, new ObjectQuery(queryStr)).Get()) { object failure = m["PredictFailure"]; //Console.WriteLine("PredictFailure: " + failure.ToString() + "\n"); disk.FailureIsPredicted = (bool)failure; //SMARTFailureRequest req = new SMARTFailureRequest("SMARTCollector"); //RequestBus.Instance.MakeRequest(req); //if (req.IsHandled) // disk.FailureIsPredicted = req.FailureIsPredicted; } // Now get the SMART attributes queryStr = string.Format("SELECT * FROM MSStorageDriver_FailurePredictData WHERE InstanceName LIKE \"%{0}%\"", drive_pnp); //Console.WriteLine(queryStr); foreach (ManagementBaseObject m in new ManagementObjectSearcher(scope, new ObjectQuery(queryStr)).Get()) { Byte[] attributes = (Byte[])m.Properties["VendorSpecific"].Value; //Console.WriteLine("Attributes length [A]: {0}", attributes.Length); int num_attributes = attributes.Length / (int)ESmartField.NumSmartFields; for (int i = 0; i < num_attributes; ++i) { try { byte[] field = new byte[(int)ESmartField.NumSmartFields]; Array.Copy(attributes, i * (int)ESmartField.NumSmartFields, field, 0, (int)ESmartField.NumSmartFields); ESmartAttribute attr = (ESmartAttribute)field[(int)ESmartField.Attribute]; if (attr == ESmartAttribute.Invalid) { continue; } //int flags = bytes[i * 12 + 4]; // least significant status byte, +3 most significant byte, but not used so ignored. // //bool advisory = (flags & 0x1) == 0x0; //bool failureImminent = (flags & 0x1) == 0x1; //bool onlineDataCollection = (flags & 0x2) == 0x2; int value = field[(int)ESmartField.Value]; //int worst = field[(int)ESmartField.Worst]; //int vendordata = BitConverter.ToInt32(field, (int)ESmartField.VendorData1); SmartAttribute resource = new SmartAttribute(attr) { Value = value }; disk.SmartAttributes.Add(resource); } catch (Exception ex) { // given key does not exist in attribute collection (attribute not in the dictionary of attributes) Console.WriteLine(ex.Message); } } } disks2.Data.Add(disk); } catch (Exception ex) { cd.SetMessage(ex); success = false; } } cd.D.Add(disks2); } else { success = false; } cd.DataIsCollected = success; return(cd); }
/// <summary> /// Queries the available system memory /// </summary> /// <param name="context"></param> private static void QueryAvailableMemory(WmiContext context) { var query = from memory in context.Source<Win32_PerfFormattedData_PerfOS_Memory>() select memory; Console.WriteLine(String.Format("Available memory: {0}MB.", query.Single().AvailableMBytes)); ulong availMemory = (from computerSystem in context.Source<Win32_ComputerSystem>() select computerSystem.TotalPhysicalMemory / 1048576).Single(); Console.WriteLine(String.Format("Available memory: {0}MB.", availMemory)); }
public string GetGPUDescription() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { return context.Source<Win32_VideoController>().First().Name; } } catch { return string.Empty; } }
public string GetMoboBrand() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return context.Source<Win32_BaseBoard>().First().Manufacturer; } catch { return string.Empty; } }
public long GetMemorySize() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { var totals = from stick in context.Source<Win32_PhysicalMemory>() select (long)stick.Capacity; return totals.Sum(); } } catch { return 0; } }
/// <summary> /// Example of querying CPUs /// </summary> /// <param name="context">The WMI query context</param> private static void QueryProcessors(WmiContext context) { var query = from processor in context.Source<Win32_Processor>() select processor; foreach (Win32_Processor processor in query) { Console.WriteLine(String.Format("DeviceID: {0}", processor.DeviceID)); Console.WriteLine(String.Format("Usage: {0}%", processor.LoadPercentage)); } }
public int GetGPUCount() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { return context.Source<Win32_VideoController>().Count(); } } catch { return 1; } }
public long GetTotalGPUMemory() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { var totals = from card in context.Source<Win32_VideoController>() select (long)card.AdapterRAM; return totals.Sum(); } } catch { return 1; } }
public long GetTotalHDDFree() { try { using (WmiContext context = new WmiContext(@"\\localhost")) { var totals = from disk in context.Source<Win32_LogicalDisk>() where disk.DriveType == 3 select (long)disk.FreeSpace; return totals.Sum(); } } catch { return 0; } }
public int GetHDDCount() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return context.Source<Win32_LogicalDisk>().Where(d => d.DriveType == 3).Count(); } catch { return 0; } }
public string GetMoboModel() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return context.Source<Win32_BaseBoard>().First().Product; } catch { return string.Empty; } }
private static void QueryProcesses(WmiContext context) { var query = from process in context.Source<Win32_Process>() select process; foreach (Win32_Process process in query) { Console.WriteLine(process.CommandLine); } }
public string GetBIOSVersion() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return context.Source<Win32_BIOS>().First().SMBIOSBIOSVersion; } catch { return string.Empty; } }
public string GetSoundcardName() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return context.Source<Win32_SoundDevice>().First().Name; } catch { return string.Empty; } }
public int GetCPUBits() { try { using (WmiContext context = new WmiContext(@"\\localhost")) return (int)context.Source<Win32_Processor>().First().AddressWidth; } catch { return 1; } }
/* * "Win32_Process","ProcessId,CommandLine", "Win32_PerfFormattedData_PerfProc_Process","IDProcess,Name,PercentProcessorTime", */ private static void Test(WmiContext context) { List<string> entires = new List<string>(); var query = from process in context.Source<Win32_Process>() join perf in context.Source<Win32_PerfFormattedData_PerfProc_Process>() on process.ProcessId equals perf.IDProcess where process.CommandLine != null && process.CommandLine.Trim().EndsWith("weblogic.Server") select String.Format("{0}:{1}", GetServerName(process.CommandLine), perf.PercentProcessorTime); foreach (var s in query) { Console.WriteLine(s); } }