October 21st, 2004

Let WMI do the heavy lifting of determining system information

Windows Management Instrumentation is a scriptable interface to configuration information. This saves you the trouble of having to figure it out yourself.

For example, here’s a little program that enumerates all the CPUs in your system and prints some basic information about them.

var locator = WScript.CreateObject(“WbemScripting.SWbemLocator”);
var services = locator.ConnectServer();
var cpus = new Enumerator(services.ExecQuery(“SELECT * FROM Win32_Processor”));
while (!cpus.atEnd()) {
  var cpu = cpus.item();
  WScript.StdOut.WriteLine(“cpu.ProcessorType=” + cpu.ProcessorType);
  WScript.StdOut.WriteLine(“cpu.CurrentClockSpeed=” + cpu.CurrentClockSpeed);
  WScript.StdOut.WriteLine(“cpu.MaxClockSpeed=” + cpu.MaxClockSpeed);
  WScript.StdOut.WriteLine(“cpu.Manufacturer=” + cpu.Manufacturer);
  WScript.StdOut.WriteLine();
  cpus.moveNext();
}

Save this program as cpus.js and run it via cscript cpus.js.

There’s a whole lot of other information kept inside WMI. You can get lost amidst all the classes that exist. The Scripting Guys have their own tool called WMI Scriptomatic which lets you cruise around the WMI namespace. (The Scripting Guys also wrote Tweakomatic which comes with hilarious documentation.)

Added 11am: It appears that people have misunderstood the point of this entry. The point here is not to show how to print the results to the screen. (I just did that to prove it actually worked.) The point is that you can let WMI do the hard work of actually digging up the information instead of having to hunt it down yourself. Want BIOS information? Try Win32_BIOS. Change the query to “SELECT * FROM Win32_BIOS” and you can get the manufacturer from the Manufacturer property. Plenty more examples in MSDN.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.