简体   繁体   English

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

[英]How to get virtual machine state on Hyper-v using Delphi or C#

Is there any component or class that allows me to get the status of all VMs running on Hyper-v? 是否有任何组件或类允许我获取在Hyper-v上运行的所有VM的状态? I want to be able to list all vms and their state (stopped, running, paused, etc.). 我希望能够列出所有vms及其状态(停止,运行,暂停等)。

I know microsoft has WMI methods, but all the samples I got are for .Net and none for Delphi. 我知道microsoft有WMI方法,但我得到的所有样本都是.Net而Delphi没有。 I should be able to convert those classes do Delphi, but it would be easier if i could use something already for Delphi. 我应该能够将这些类转换为Delphi,但如果我可以使用已经用于Delphi的东西会更容易。

EDIT 编辑

I have a sample in C#: 我在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());
}

I tried to run this on Visual Studio to see if it works so I can try to translate it to Delphi. 我尝试在Visual Studio上运行它以查看它是否有效,因此我可以尝试将其转换为Delphi。 But even though i change the username, domain and password I still got this error: 但即使我更改了用户名,域名和密码,我仍然遇到此错误:

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

The most up to date Delphi for WMI is Rodrigos components : 最新的Delphi for WMI是Rodrigos组件:

wmi-delphi-code-creator WMI-Delphi的代码创造者

and

object-pascal-wmi-class-generator 对象的Pascal-WMI级发电机

There's free Delphi code for accessing WMI at Magenta Systems , in MagWMI . 有一些免费的Delphi代码可以在MagWMI的 Magenta Systems上访问WMI。 It comes with full source, including a demo app that lets you run WMI queries. 它附带完整的源代码,包括一个允许您运行WMI查询的演示应用程序。 It's current web page (linked above) says it's compatible with current Windows (and Delphi) versions. 它的当前网页(上面链接)表示它与当前的Windows(和Delphi)版本兼容。

I don't know if it specifically works with virtualization, but it will at least give you the start of using WMI from Delphi code. 我不知道它是否特别适用于虚拟化,但至少可以让您开始使用Delphi代码中的WMI。 (EDIT: It appears that the demo is documented as only working on the local computer so that fewer parameters have to be passed, to make the demo more understandable. It still shows the basics of using WMI with Delphi, however, so it should get you on your way.) (编辑:似乎演示文档只记录在本地计算机上,因此必须传递更少的参数,以使演示更容易理解。但它仍然显示了使用WMI与Delphi的基础知识,所以它应该得到你在路上。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM