Is there any component or class that allows me to get the status of all VMs running on Hyper-v? I want to be able to list all vms and their state (stopped, running, paused, etc.).
I know microsoft has WMI methods, but all the samples I got are for .Net and none for Delphi. I should be able to convert those classes do Delphi, but it would be easier if i could use something already for Delphi.
EDIT
I have a sample in 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. But even though i change the username, domain and password I still got this error:
{"The RPC server is not available. (HRESULT: 0x800706BA)"}
There’s free Delphi code for accessing WMI at Magenta Systems, in MagWMI. It comes with full source, including a demo app that lets you run WMI queries. It’s current web page (linked above) says it’s compatible with current Windows (and Delphi) versions.
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. (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.)