1

是否有任何组件或类可以让我获得在 Hyper-v 上运行的所有 VM 的状态?我希望能够列出所有虚拟机及其状态(停止、运行、暂停等)。

我知道微软有 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)"}
4

2 回答 2

3

用于 WMI 的最新 Delphi 是 Rodrigos 组件:

wmi-delphi-代码创建器

对象帕斯卡 wmi 类生成器

于 2011-10-26T18:33:48.373 回答
1

MagWMI中的Magenta Systems提供了用于访问 WMI 的免费 Delphi 代码。它带有完整的源代码,包括一个可让您运行 WMI 查询的演示应用程序。它的当前网页(上面链接)说它与当前的 Windows(和 Delphi)版本兼容。

我不知道它是否特别适用于虚拟化,但它至少可以让您开始使用 Delphi 代码中的 WMI。(编辑:似乎该演示记录为仅在本地计算机上工作,因此必须传递更少的参数,以使演示更易于理解。但是,它仍然显示了将 WMI 与 Delphi 一起使用的基础知识,所以它应该得到你在路上。)

于 2011-10-26T18:04:56.210 回答