Connecting to WMI with PHP - The Safe Command Line “format” Command
(Page 3 of 5 )
This is by far the most useful part of WMI in all my experience with it. By default the data returned by the WMI is dynamically tab delimited, which means you never really know how many tabs or spaces that it’s going to have between your data. So what we want to do is take a look at the /format switch for the WQL. /format lets us specify a XSL file that contains formatting instructions. XSL is an XML Stylesheet that is commonly used to format data into a more readable form, usually XML. But for sake of this article we’ll (of course) be using it to format our WMI data. The first thing you need to know about your formatting files is that they are stored in a folder named wbem which can be found in your system32 folder. They have a few that you can use and edit already made; the only ones that I found that are useful for passing data to PHP are the RAWXML and the CSV formats. The CSV format is comma delimited so that you can load all the data into an array and parse it out. The RAWXML format is nice if you want to parse the data like XML.
I like to use the data formatted as XML because I can output the data easily with simpleXML, which is part of PHP5. There were a few problems using the RAWXML style that is packaged with Windows because it didn’t output it in a very useful or organized fashion -- well, not well enough my taste. So I quickly hacked my way through the RAWXML style to make my own style. I don’t personally know much about XSL, but I found that it wasn’t very hard to create my own style from editing one of the default one’s. Here’s the one I came up with.
<?xml version="1.0"?>
<!-- Copyright (c) Microsoft Corporation. All rights reserved. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="RESULTS">
<xsl:for-each select="CIM/INSTANCE">
<xsl:sort select="PROPERTY[@NAME='Name']|PROPERTY.ARRAY[@NAME='Name']|PROPERTY.REFERENCE[@NAME='name']"/>
<xsl:for-each select="PROPERTY|PROPERTY.ARRAY|PROPERTY.REFERENCE">
<xsl:variable name="typevar" select="@TYPE"/>
<xsl:copy><xsl:value-of select="@NAME"/></xsl:copy>
<xsl:for-each select="VALUE|VALUE.ARRAY|VALUE.REFERENCE">
<xsl:apply-templates select=".">
<xsl:with-param name="type">
<xsl:value-of select="$typevar"/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
<xsl:template match="VALUE">
<xsl:param name="type"/>
<xsl:param name="includequotes"/>
<xsl:param name="isarray"/>
<xsl:choose>
<xsl:when test="$type='string'">
<xsl:if test="$includequotes='true'">"</xsl:if><xsl:copy><xsl:value-of select="."/></xsl:copy><xsl:if test="$includequotes='true'">"</xsl:if>
</xsl:when>
<xsl:when test="$type='char16'">
<xsl:copy><xsl:value-of select="."/></xsl:copy>
</xsl:when>
<xsl:when test="$type='uint16'or $type='uint32' or $type='uint64' or $type='uint8' or $type='real32'or $type='real64' or $type='sint16'or $type='sint32' or $type='sint64' or $type='sint8'">
<xsl:if test="not($isarray='true')"><xsl:copy><xsl:value-of select="."/></xsl:copy></xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:copy><xsl:value-of select="."/></xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="/" >
<element>
<xsl:apply-templates select="COMMAND/RESULTS"/>
</element>
</xsl:template>
</xsl:stylesheet>
This will output your data in a really clean XML format with the name of the property in the <property></property> tags and the value of it in the <value></value> tags. Here is the output after running a query to get the freephysicalmemory and the freevirtualmemory:
<?xml version=”1.0” encoding=”UTF-16”?> <element><property>FreePhysicalMemory</property><value>513128
</value></element><element><property>FreeVirtualMemory</property>
<value>2708708</value></element>
Now, here’s an example of the same thing with the CSV format
Node,FreePhysicalMemory,FreeVirtualMemory
NS1,513128,2712180
As you can see, the XML is a lot easier to work with because of how I’ve grouped the names and values, unlike the CSV, not to mention it can easily be parsed with simpleXML.
Next: Spit it Out >>
More Windows Scripting Articles
More By James Murray