Git Product home page Git Product logo

wmi's Introduction

WMI - Windows Management Instrumentation in PHP

Travis CI Scrutinizer Code Quality Total Downloads Latest Stable Version

Requirements

To use WMI, your server must meet the following requirements:

  • Windows OS
  • PHP 5.4 or Greater
  • PHP COM extension enabled

Installation

Insert WMI into your composer.json:

"stevebauman/wmi": "1.1.*"

Now run composer update.

You're all set!

Usage

Connecting

To use WMI, you must create a new WMI instance. To interact with the current computer, just create a WMI instance:

use Stevebauman\Wmi\Wmi;
    
$wmi = new Wmi();

To interact with a PC on your network, you'll need to enter a host name, and a username and password if needed:

$wmi = new Wmi($host = 'GUEST-PC', $username = 'guest', $password = 'password');

Now we can connect to it, but you'll need to specify the namespace you're looking to connect to:

$connection = $wmi->connect('root\\cimv2');

The connect() method will return a Stevebauman\Wmi\Connection instance if the connection was successful:

if($connection = $wmi->connect('root\\cimv2')) {
    echo "Cool! We're connected.";
    
    $query = $connection->newQuery();
} else {
    echo "Uh oh, looks like we couldn't connect.";
}

Querying

CAUTION: Before we get started with queries, you should know that NO VALUES are escaped besides quotes inside any query method. This package is not meant to handle user input, and you should not allow users to query computers / servers on your network.

This is due to WMI not supporting parameterization in queries.

Raw Queries

Once you've connected to the computer, you can execute queries on it with its connection. To execute a raw query, use:

if($connection = $wmi->connect('root\\cimv2')) {
    $results = $connection->query('SELECT * FROM Win32_LogicalDisk');

    foreach($results as $disk) {
        $disk->Size;
    }
}

Query Builder

WMI Comes with a WQL query builder so you're able to easily build statements. To create a new Builder use the newQuery() method on the WMI connection instance like so:

$query = $wmi->getConnection()->newQuery();

Once you have the query, we can start building:

// Executes the query "select * from Win32_LogicalDisk where Size >= '150000'"
$results = $query->from('Win32_LogicalDisk')
    ->where('Size', '>=', '150000')
    ->get();
Select

The select method accepts a string or an array to insert selects onto the current query. For example:

// Select All
$query->select('*');

// Select Specific (array)
$query->select(['Name', 'Disk', 'Size']);

// Select Specific (multiple args)
$query->select('Name', 'Disk', 'Size');

// Select Specific (string)
$query->select('Name, Disk, Size');

If you don't use the select method, that's fine too. The Builder will assume you're meaning to select all columns, so you're able to perform:

$query->from('Win32_LogicalDisk')->get();

// Query Executed
select * from Win32_LogicalDisk
From

The from method accepts a string that is a WMI class name. For example:

$query->from('Win32_DiskDrive')->get();

//

$query->from('Win32_BIOS')->get();

For more information on WMI classes, visit: https://msdn.microsoft.com/en-us/library/aa394132(v=vs.85).aspx

Where

The where method accepts a field, operator and value. This is useful for retrieving data with a specific set of requirements.

The method:

$query->where($field, $operator, $value);

Example:

$query->where('Size', '>', 15000)->from('Win32_LogicalDisk')->get();

The field parameter needs to be an attribute in the from class, otherwise you will not receive any results.

wmi's People

Contributors

scrutinizer-auto-fixer avatar stevebauman avatar webzes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

wmi's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.