Windows Scripting
  Home arrow Windows Scripting arrow Page 4 - 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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 - Spit it Out


    (Page 4 of 5 )

    Now, here’s something that’s interesting to know -- you can’t use the exec function to work with WMI, it won’t output. Instead you have to use the passthru function. This is because the passthru function runs the program and returns the raw output. Exec doesn’t do that, however you can use passthru in the exact same way you use exec so you don’t really need to worry about anything. To simply send your information to the browser all you’ll have to do is this.

    echo passthru('wmic cpu get loadpercentage /format:newxml');

    That’s it, no big deal, right!? Well, no, that’s not really it. That will work if you want to look at the source of the page every time to see the data, which I don’t think you’d want to do -- you’ll have to make it parse the data with an XML parser or simpleXML. I prefer simpleXML because it’s, well, simple. So I came up with the small little function: 

    function parse_stat($instance){

    ob_start();

    passthru('wmic '.$instance.' /format:newxml');

    $results=ob_get_clean();

    $xml[] = simplexml_load_string($results)

    Return $xml;

    }

    This will return an array of all my WMI data which I can then put in a foreach loop and echo out, where I want it. This function is also fairly nice because you only have to tell it the WQL string, no formatting data. This is how it’s used.

    parse_stat("os get FreePhysicalMemory,TotalVirtualMemorySize,FreeVirtualMemory,
    TotalVisibleMemorySize");

    parse_stat("cpu get loadpercentage");

    parse_stat("logicaldisk where drivetype=3 get size,name,freespace");

    foreach ($xml as $wmi){

    echo $wmi->property.”: “.$wmi->value;

    }

    And that is just about all you need to know to get started working with WMI from PHP.

    More Windows Scripting Articles
    More By James Murray


       · $xml[] = simplexml_load_string($results) - where's the semicolon...
     

    WINDOWS SCRIPTING ARTICLES

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek