Connecting to WMI with PHP - The Newest SQL Slang
(Page 2 of 5 )
SQL has been around for a long time, but now there’s a newer query language called WQL. WQL stands for WMI Query Language, and I bet you can guess what it’s used for. WQL isn’t hard to learn at all. The hard part is getting used to how much it seems like SQL while it’s not SQL at all. WQL is actually a subset of SQL with minor changes so that it will work with WMI. A table containing all the keywords that work with WQL can be found here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/sql_for_wmi.asp
After you get comfortable with WQL, we’ll start talking about how to find out what information is available from the WMI.
The easiest way I’ve actually found to list the available information provided by WMI is to open a command prompt and type in “wmic”. This will launch the command line interface to WMI. After the wmic has started type in a simple /? and it will list out all the available aliases that you can use. If you then type in the name of one of the aliases at the wmic command prompt, it will then show you all the information that can be gathered from that particular alias. For example if you type in “CPU” at the prompt it will show you a table that contains all the data about the CPU, such as the address width, the architecture, the caption name and so on. This information isn’t very important to most people, so what we’ll want to do is use a little bit of WQL to get some useful information. If you go back to the command prompt and type in “CPU get LoadPercentage” ,it will show a little table with only the current load percentage in it.
The most useful aliases I’ve found in WMI are OS, CPU, and LOGICALDISK. Here are some examples of WQL for these.
This one will get the amount of free RAM, total RAM, free virtual memory, and total virtual memory, and return the value’s in bytes.
os get FreePhysicalMemory,TotalVirtualMemorySize,
FreeVirtualMemory,TotalVisibleMemorySize
This returns the current CPU load in a percentage.
cpu get loadpercentage
This one will return the total size the name, and the free space in byte for each hard drive partition on your system.
logicaldisk where drivetype=3 get size,name,freespace
Now that we’ve talked about how to get some information, let’s talk about how to get our data formatted for use with PHP.
Next: The Safe Command Line “format” Command >>
More Windows Scripting Articles
More By James Murray