【问题标题】:How can I use WMI from PHP如何从 PHP 中使用 WMI
【发布时间】:2012-02-16 10:06:07
【问题描述】:

首先,对于新手的问题,我很抱歉! :D

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

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

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

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

谢谢..

【问题讨论】:

  • 能否请您简要介绍一下链接的内容。可能会让人们更愿意考虑你的问题。
  • 他会建立一个网站来管理虚拟机,例如 creat/start/stop.... 虚拟机使用 WMI API for Hyper-v with asp.net 谢谢 Gordon
  • 这与 Hyper-V 无关——HyperV 通过标准的windoqws API (WMI) 公开,以及其他许多事情。所以,问题是“我如何从 PHP 中使用 WMI”。重新开始。

标签: php wmi


【解决方案1】:
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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多