我如何使用PHP的WMI

时间:2012-02-16 10:06:08

标签: php wmi

首先,我很抱歉新问题! :d

我想构建一个网站,以完全按照以下主题中的psand所做的:

http://social.technet.microsoft.com/Forums/en/windowsserver2008r2virtualization/thread/697eafc2-7778-488b-8774-7554f84de642

他建立了一个网站来管理虚拟机器,例如creat / start / stop ....使用WMI API for Hyper-v与asp.net的虚拟机

现在他用ASP.NET做到了,我的问题是我可以用PHP做到吗? 换句话说,API是否支持PHP?

谢谢..

1 个答案:

答案 0 :(得分:0)

class wmiConnect
{
    // WMI connection to specified host
    protected $connection;
    /**
     * Create a new wmi instance.
     *
     * @param   string  $host       Host name or IP address to connect to
     * @param   string  $username   Local host user with rights to query WMI; normally a local admin
     * @param   string  $password   Password of local user account
     * @return  void                New wmi object
     */
    public function __construct($host = null, $username = null, $password = null) {
        $wmiLocator = new \COM('WbemScripting.SWbemLocator');
        try {
            $this->connection = $wmiLocator->ConnectServer($host, 'root\\CIMV2', $username, $password);
            $this->connection->Security_->impersonationLevel = 3;
        } catch (\Exception $e) {
            // -2147352567 means that we're unable to connect to the local host with a username and password.
            // Attempt connection again passing null values for username and password.
            if ($e->getCode() == '-2147352567') {
                $this->connection = $wmiLocator->ConnectServer($host, 'root\CIMV2', null, null);
                $this->connection->Security_->impersonationLevel = 3;
            }
        }
    }
    /**
     * Get all properties of a WMI class.
     *
     * @param   string  $win32_class    Win32 class to retrieve data from
     * @return  object                  WMI collection object
     */
    public function getInfo($win32_class) {
        $WMIcollection = $this->connection->ExecQuery('SELECT * FROM ' . $win32_class);
        foreach ($WMIcollection as $WMIobj) {
            return $WMIobj;
        }
    }

}

以下是WMI的类列表: https://msdn.microsoft.com/en-us/library/aa394132(v=vs.85).aspx

2012年的Hyper-v有一个新的命名空间 - https://msdn.microsoft.com/en-us/library/hh850078(v=vs.85).aspx