PHP读取WMI最兼容的代码.

分类
代码库
更新时间
2018-09-29 14:03:45

PHP需要加载com_dotnet.dll.

一次性查询返回所有结果

自动转化编码.


<?php

$wmi = new COM('winmgmts://127.0.0.1/root/CIMV2',null,CP_UTF8);
/*
$clsList = array();
// 所有class
$sub = $wmi->SubclassesOf();
foreach($sub AS $k => $subData){
    $keys = $subData->path_->Class;
    $clsList[] = $keys;
}
*/

$processes = $wmi->ExecQuery('SELECT * FROM Win32_OperatingSystem'); // Win32_Service, Win32_Process , Win32_OperatingSystem
$Data = array();
foreach($processes as $pkey => $process){
    // com_print_typeinfo($process);
    $row = array();
    foreach($process->Properties_ AS $key => $val){
        if(is_object($val->value)){
            $tval = array();
            if(count($val->value)>0){
                foreach($val->value AS $k => $v){
                    $tval[$k] = $v;
                }
                $kval = json_encode($tval);
            }else{
                $kval = null;
            }
            $kname = $val->name;
        }else{
            $kname = $val->name;
            $kval = $val->value; 
        }
        $row[$kname] = $kval;
    }
    $Data[$pkey] = $row; 
}

echo '<pre>';
print_r($Data);


END


分享工作室[粤ICP备16004795号-2] 腾讯云 / 腾讯AI / 南沙新区 / 信用 0.012