如何从 PHP 使用 WMI

发布于 2025-01-05 18:39:36 字数 513 浏览 1 评论 0原文

首先我对新手问题感到抱歉! :D

我想建立一个网站来完成以下主题中 psand 的操作:

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

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

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

谢谢 ..

First of all I am sorry for the newbi question ! :D

I want to build a website to do excactly what psands in the following topic did:

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

he built a website to manage virtual machiens such as creat/start/stop.... VMs using the WMI API for Hyper-v with asp.net

now he did it with ASP.NET , my question is can I do it with PHP ?
in another words does the API support PHP ?

Thanks ..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪心 2025-01-12 18:39:37
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/库/hh850078(v=vs.85).aspx

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;
        }
    }

}

Here is a list of classes for WMI:
https://msdn.microsoft.com/en-us/library/aa394132(v=vs.85).aspx

Hyper-v for 2012 on has a new namespace - https://msdn.microsoft.com/en-us/library/hh850078(v=vs.85).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文