Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - Connecting to WMI with PHP
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

Connecting to WMI with PHP
By: James Murray
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 23
    2004-07-06

    Table of Contents:
  • Connecting to WMI with PHP
  • The Newest SQL Slang
  • The Safe Command Line “format” Command
  • Spit it Out
  • Just a Few More Words

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Windows Scripting Articles
    More By James Murray


     

    WINDOWS SCRIPTING ARTICLES

    - Introducing Two-Way Data Binding using Silve...
    - Silverlight 2.0 Application Development with...
    - Burning Multisession CDs with IMAPI2 in WSH
    - Creating a Silverlight 2.0 Application that ...
    - Burning CDs with the IMAPI2 Control
    - Burning CDs in Windows XP with WSH
    - Advanced Word Object Scripting
    - Reading and Printing Word Documents in WSH
    - Scripting Microsoft Word
    - Using WSH to Catalog MP3 Files
    - Reading MP3 ID3 Tags in WSH
    - A Brief Look at Menus in WPF
    - More Examples of Simplified Image Processing...
    - Completing a WPF To-Do List Application
    - Simplified Image Processing in GDI+





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT