Comment programmer Hyper-V avec WMI et C#
Vous trouverez
sur le blog de Richard Macdonald une introduction sur la programmation sur Hyper-V.
Par exemple:
Connexion
// add this namespace in order to access WMI
// also need to add a reference to System.Management.dll
using System.Management;
// we want to connect locally (".") to the Hyper-V namespace ("root\virtualization")
ManagementScope manScope = new ManagementScope(@"\\.\root\virtualization");
// 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");
// connect and set up our search
ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj);
ManagementObjectCollection vmCollection = vmSearcher.Get();
// loop through the machines
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());
}
Authentification avec un autre utilisateur
// add this namespace in order to access WMI
// also need to add a reference to System.Management.dll
using System.Management;
// 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());
}
Pour d’autres exemples et la continuation suivez le blog de Richard :
http://blogs.technet.com/richard_macdonald/archive/2008/08/11/3103559.aspx
Roxana Panait, MSFT ________ Votez l’article qui vous est utile ou postez un pour participer au concours : Appel a la contribution! Publiez un tip ou un petit tutorial (comment faire) sur la technologie que vous connaissez le mieux ! - http://social.technet.microsoft.com/Forums/fr-FR/1635/thread/c0fc6847-a4b0-4253-85e9-8eac0cc95aa0