Skip to main content
 首页 » 编程设计

c#之如何使用 Delphi 或 C# 在 Hyper-v 上获取虚拟机状态

2024年02月23日23zdz8207

是否有任何组件或类允许我获取在 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 上运行它以查看它是否有效,以便我可以尝试将它转换为 Delphi。但即使我更改了用户名、域和密码,我仍然收到此错误:

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

请您参考如下方法:

WMI 的最新 Delphi 是 Rodrigos 组件:

wmi-delphi-code-creator

和

object-pascal-wmi-class-generator