2011-10-26 61 views
1

是否有任何組件或類可以獲取在Hyper-v上運行的所有VM的狀態?我希望能夠列出所有vms及其狀態(停止,運行,暫停等)。如何使用Delphi或C獲取Hyper-v上的虛擬機狀態?

我知道微軟有WMI方法,但我得到的所有樣本都是針對.Net,而沒有針對Delphi。我應該可以將這些類轉換爲Delphi,但如果我可以使用Delphi的某些東西,那將會更容易。

編輯

我有C#示例:

/

/ define the information we want to query - in this case, just grab all properties of the object 
ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem"); 

// object for storing WMI connection options 
// pass the "user", "password" and "domain" from command-line 
// don't hard-code these into the application! 
ConnectionOptions connOpts = new ConnectionOptions(); 
connOpts.Username = user; 
connOpts.Authority = "ntlmdomain:" + domain; 
connOpts.Password = password; 

// management scope object 
ManagementScope manScope = new ManagementScope(@"\\RemoteSystem\root\virtualization", connOpts); 

// connect and set up our search 
ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj); 
ManagementObjectCollection vmCollection = vmSearcher.Get(); 

// loop through the VMs 
foreach (ManagementObject vm in vmCollection) 
{ 
    // display VM details 
    Console.WriteLine("\nName: {0}\nStatus: {1}\nDescription: {2}\n", 
         vm["ElementName"].ToString(), 
         vm["EnabledState"].ToString(), 
         vm["Description"].ToString()); 
} 

我嘗試在Visual Studio中運行這個,看看它的工作原理,所以我可以試着翻譯一下到德爾福。但是,即使我更改用戶名,域名和密碼,我仍然有這個錯誤:

{"The RPC server is not available. (HRESULT: 0x800706BA)"} 
+0

我不熟悉的WMI方法獲取這些特定的數據,但是在各種語言中有許多WMI示例,而不僅僅是.NET。難道你不能將這些例子中的信息與你在VM交互中找到的.NET特定的例子結合起來嗎? – GolezTrol

+0

有一個關於WMI和Delphi的好博客:http://theroadtodelphi.wordpress.com/ – Andreas

+1

等一下。你說你可以找到很多.Net的例子來說明如何做到這一點。你現在可以用C#而不是僅僅用Delphi來接收答案。那麼你的問題是什麼? –

回答

1

有在Magenta Systems訪問WMI,在MagWMI免費Delphi代碼。它帶有完整的源代碼,包括一個可讓您運行WMI查詢的演示應用程序。這是當前的網頁(上面鏈接)說,它與當前的Windows(和Delphi)版本兼容。

我不知道它是否特別適用於虛擬化,但它至少會爲您提供從Delphi代碼中使用WMI的開始。 (編輯:看來演示被記錄爲僅在本地計算機上工作,因此需要傳遞更少的參數,以使演示更易於理解,但它仍然顯示了使用Delphi的WMI的基礎知識,所以它應該得到你對你的方式)

相關問題