Math operations in a WMI Query clause

In my console application (.NET), I execute a WMI request as follows:

ManagementObjectSearcher query; ObjectQuery oq; ManagementObjectCollection objectCollection; try { oq = new ObjectQuery("SELECT TotalVisibleMemorySize, FreePhysicalMemory FROM Win32_OperatingSystem"); query = new ManagementObjectSearcher(oq); objectCollection = query.Get(); } catch { return null; } return objectCollection; 

I am splitting data collected for use in my application. It currently works great for me.
I am using multiple wmi requests for my application. To make one method to execute all my wmi requests from config, I will need to do the division in the select clause of the request. I need to execute a WMI request, for example:

 SELECT ((TotalVisibleMemorySize)/1024) as TotalVisibleMemorySize1, ((FreePhysicalMemory)/1024) as FreePhysicalMemory1 FROM Win32_OperatingSystem 

For this query, I get an Invalid query error.
Is there a syntax error in this query or is it not possible to split in the select clause of a WMi request?

+4
source share
1 answer

WMI doesn't seem to support complex queries. Your best bet is to capture wmi results and convert as needed:

 foreach (ManagementObject mo in objectCollection) { Console.WriteLine("Total Memory = {0} MB", Convert.ToInt32(mo.GetPropertyValue("TotalVisibleMemorySize"))/1024); } 
+2
source

All Articles