2012-02-16 47 views
0

首先,我對newbi問題感到抱歉! :d我如何使用PHP中的WMI

我想建立一個網站,做什麼excactly的psands以下主題中所做的:

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

,他建立了一個網站來管理虛擬machiens如科瑞/啓動/停止... 。使用用於Hyper-v的WMI API的虛擬機使用asp.net

現在他用ASP.NET做了它,我的問題是我可以用PHP來做到嗎? 換句話說API支持PHP嗎?

謝謝..

+0

可以請您給一個簡短的鏈接內容摘要。可能會讓人們更願意考慮你的問題。 – Gordon 2012-02-16 10:10:07

+0

他會建立一個網站來管理虛擬machiens,如creat/start/stop ....虛擬機使用Hyper-V的WMI API與asp.net 感謝戈登 – user1213305 2012-02-16 10:16:05

+0

通過標準windoqws API(WMI)公開,和其他許多事情一樣。所以,問題是「我如何使用PHP中的WMI」。 Retgging。 – TomTom 2012-02-16 10:30:56

回答

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

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