【问题标题】:How to get virtual machine state on Hyper-v using Delphi or C#如何使用 Delphi 或 C# 在 Hyper-v 上获取虚拟机状态
【发布时间】:2011-10-26 15:43:50
【问题描述】:

是否有任何组件或类可以让我获得在 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)"}

【问题讨论】:

  • 我不熟悉用于获取这些特定数据的 WMI 方法,但是有许多不同语言的 WMI 示例,不仅是 .NET。您不能将这些示例中的信息与您找到的用于 VM 交互的 .NET 特定示例结合起来吗?
  • 有一篇关于WMI和Delphi的好博客:theroadtodelphi.wordpress.com
  • 等等。你说你可以找到很多关于如何做到这一点的 .Net 示例。您现在可以接受 C# 中的答案,而不仅仅是 Delphi。那么你的问题是什么?
  • @Rafael Colucci:你能在这里分享你的发现吗?

标签: c# delphi wmi virtualization hyper-v


【解决方案1】:

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

wmi-delphi-code-creator

object-pascal-wmi-class-generator

【讨论】:

  • 谢谢...我不知道 RRUZ 有 WMI 的东西。我想我需要花更多的时间在他的博客上。 :)
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    相关资源
    最近更新 更多