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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
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 / 22
    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


     

    WINDOWS SCRIPTING ARTICLES

    - A Portable Scripting Toolbox
    - WPF Through an Example: Introduction
    - Beginning SharePoint Web Part Development
    - More Alternative Languages for WSH
    - WPF Control Layout
    - WSH in Other Languages
    - Screen Capturing via GDI+ and GDI
    - Understanding Procedures in VBScript
    - Printing Documents in WSH
    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway