Getting started with Windows Management Instrumentation
- 13 April, 2004 07:57
Most people are accustomed to managing Windows XP Professional the point-and-click way. However, thanks to the Windows Management Instrumentation (WMI) technology, Microsoft’s implementation of the Web Based Enterprise Management (WBEM) standard published by the Distributed Management Task Force (www.dmtf.org), you can also use the command line to manage Windows XP Professional.
WMI allows you to manage computers locally or remotely over a network. It uses XML containers or databases, formatted according to the Common Information Model (CIM) standard schema. The files for the WBEM implementation in Windows XP Professional are in %systemroot%\System32\Wbem by default.
Working directly with WMI schemas and classes is cumbersome, so Microsoft provides the WMIC (WMI command-line) tool as an interface to them. WMIC comes with a set of built-in aliases which makes it easier to get to the WMI classes and use them for common management tasks. WMIC can be used either interactively, which puts in a shell mode with a prompt, or non-interactively for quick tasks or inclusion in scripts (see here for a screenshot).
To start up WMIC, simply open a CMD shell and type “wmic” into it. You’ll see this prompt:
which tells you that WMIC is active, and that you’re at the top-level ‘role’ or namespace on your computer. To see which aliases are available, type /? for a full listing; each alias comes with online help, again accessible with the /? switch, like this:
CPU - CPU management.
HINT: BNF for Alias usage.
(<alias> [WMIObject] | <alias> [<path where>] | [<alias>] <path where>) [<verb clause>].
CPU ASSOC [<format specifier>]
CPU CREATE <assign list>
CPU GET [<property list>] [<get switches>]
CPU LIST [<list format>] [<list switches>]
If you then, for example, type cpu get /? you’ll get the full property list retrievable for that sub-command, as well as the various switches for output formatting it supports.
The CREATE and DELETE command obviously don’t do anything of the kind with the physical devices in your computer; instead, they work on the WMI classes, so be careful when using them.
Here’s an example of how to select data from the ‘get’ sub-command output:
wmic:root\cli>cpu get manufacturer, maxclockspeed, l2cachesize
L2CacheSize Manufacturer MaxClockSpeed
1024 GenuineIntel 1395
WMIC comes into its own when used for managing systems over a network. Provided security policies grant you access and Remote Procedure Calls are available, managing networked computers is as easy as:
Another nice feature of WMIC is that it allows you to save the command output in TXT, HTML, XML, CSV and other formats. Here’s a quick way to create an HTML table with all the Hotfixes and updates installed on your computer listed:
C:\wmic /output:c:\qfelist\qfe.html qfe get /format:htable.xsl
Note that, currently, WMIC doesn’t handle paths with spaces in them. Use the /APPEND alias to add additional listings to the output file. You can do more than just data collection and report formatting with WMIC. This command kills a hung process, program.exe, on the computer Derek:
c:\wmic /node:”derek” process where name=”program.exe” delete
Windows XP Professional comes with a GUI tool to browse schemas on your local and remote computers: the WMI Tester program (wbemtest.exe). You can use WBEMtest to view all the 900-plus classes provided with Windows XP Professional, create and delete new ones, as well as execute methods, run queries and generally learn more about WMI. The WMI Tester comes with good documentation in a Windows Help file, which is an added bonus.
Please note that these tips require Administrator security context for the full set of commands and, in some cases, give access to low-level configuration of your computer which, if set wrongly, could render it inoperable. Back up your system before implementing!