using System; using System.Collections.Generic; using System.Management; namespace MyNamespace { #region Return Value of RequestStateChange Method of the Msvm_ComputerSystem Class //Return Value of RequestStateChange Method of the Msvm_ComputerSystem Class //This method returns _disibledevent=>//Completed with No Error (0) //DMTF Reserved (7–4095) //Method Parameters Checked - Transition Started (4096) //Failed (32768) //Access Denied (32769) //Not Supported (32770) //Status is unknown (32771) //Timeout (32772) //Invalid parameter (32773) //System is in use (32774) //Invalid state for this operation (32775) //Incorrect data type (32776) //System is not available (32777) //Out of memory (32778) #endregion public class VMManagement { private static string hostServer = "hostServer"; private static string userName = "username"; private static string password = "password"; public static string HostServer { get; set; } public static string UserName { get; set; } public static string Password { get; set; } public static VMState GetVMState(string vmName) { VMState vmState = VMState.Undefined; ConnectionOptions co = new ConnectionOptions(); co.Username = userName; co.Password = password; ManagementScope manScope = new ManagementScope(string.Format(@"\\{0}\root\virtualization", hostServer), co); manScope.Connect(); ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem"); ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj); ManagementObjectCollection vmCollection = vmSearcher.Get(); foreach (ManagementObject vm in vmCollection) { if (string.Compare(vm["ElementName"].ToString(), vmName, true) == 0) { vmState = ConvertStrToVMState(vm["EnabledState"].ToString()); break; } } return vmState; } public static bool StartUp(string vmName) { return ChangeVMState(vmName, VMState.Enabled); } public static bool ShutDown(string vmName) { return ChangeVMState(vmName, VMState.Disabled); } public static bool RollBack(string vmName, string snapShotName) { ConnectionOptions co = new ConnectionOptions(); co.Username = userName; co.Password = password; ManagementScope manScope = new ManagementScope(string.Format(@"\\{0}\root\virtualization", hostServer), co); manScope.Connect(); ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem"); ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj); ManagementObjectCollection vmCollection = vmSearcher.Get(); object opResult = null; // loop the virtual machines foreach (ManagementObject vm in vmCollection) { // find the vmName virtual machine, then get the list of snapshot if (string.Compare(vm["ElementName"].ToString(), vmName, true) == 0) { ObjectQuery queryObj1 = new ObjectQuery(string.Format("SELECT * FROM Msvm_VirtualSystemSettingData WHERE SystemName='{0}' and SettingType=5", vm["Name"].ToString())); ManagementObjectSearcher vmSearcher1 = new ManagementObjectSearcher(manScope, queryObj1); ManagementObjectCollection vmCollection1 = vmSearcher1.Get(); ManagementObject snapshot = null; // find and record the snapShot object foreach (ManagementObject snap in vmCollection1) { if (string.Compare(snap["ElementName"].ToString(), snapShotName, true) == 0) { snapshot = snap; break; } } ObjectQuery queryObj2 = new ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService"); ManagementObjectSearcher vmSearcher2 = new ManagementObjectSearcher(manScope, queryObj2); ManagementObjectCollection vmCollection2 = vmSearcher2.Get(); ManagementObject virtualSystemService = null; foreach (ManagementObject o in vmCollection2) { virtualSystemService = o; break; } if (ConvertStrToVMState(vm["EnabledState"].ToString()) != VMState.Disabled) { ShutDown(vm["ElementName"].ToString()); } opResult = virtualSystemService.InvokeMethod("ApplyVirtualSystemSnapShot", new object[] { vm.Path, snapshot.Path }); break; } } return "0" == opResult.ToString(); } public static List
v虚拟机,C# 实现管理Hyper-V 虚拟机
using System; using System.Collections.Generic; using System.Management; namespace MyNamespace { #region Return Value of RequestStateChange Method of the Msvm_ComputerSystem Class //Return Value of RequestStateChange Method of the Msvm_ComputerSystem Class //This method returns _disibledevent=>//Completed with No Error (0) //DMTF Reserved (7–4095) //Method Parameters Checked - Transition Started (4096) //Failed (32768) //Access Denied (32769) //Not Supported (32770) //Status is unknown (32771) //Timeout (32772) //Invalid parameter (32773) //System is in use (32774) //Invalid state for this operation (32775) //Incorrect data type (32776) //System is not available (32777) //Out of memory (32778) #endregion public class VMManagement { private static string hostServer = "hostServer"; private static string userName = "username"; private static string password = "password"; public static string HostServer { get; set; } public static string UserName { get; set; } public static string Password { get; set; } public static VMState GetVMState(string vmName) { VMState vmState = VMState.Undefined; ConnectionOptions co = new ConnectionOptions(); co.Username = userName; co.Password = password; ManagementScope manScope = new ManagementScope(string.Format(@"\\{0}\root\virtualization", hostServer), co); manScope.Connect(); ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem"); ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj); ManagementObjectCollection vmCollection = vmSearcher.Get(); foreach (ManagementObject vm in vmCollection) { if (string.Compare(vm["ElementName"].ToString(), vmName, true) == 0) { vmState = ConvertStrToVMState(vm["EnabledState"].ToString()); break; } } return vmState; } public static bool StartUp(string vmName) { return ChangeVMState(vmName, VMState.Enabled); } public static bool ShutDown(string vmName) { return ChangeVMState(vmName, VMState.Disabled); } public static bool RollBack(string vmName, string snapShotName) { ConnectionOptions co = new ConnectionOptions(); co.Username = userName; co.Password = password; ManagementScope manScope = new ManagementScope(string.Format(@"\\{0}\root\virtualization", hostServer), co); manScope.Connect(); ObjectQuery queryObj = new ObjectQuery("SELECT * FROM Msvm_ComputerSystem"); ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(manScope, queryObj); ManagementObjectCollection vmCollection = vmSearcher.Get(); object opResult = null; // loop the virtual machines foreach (ManagementObject vm in vmCollection) { // find the vmName virtual machine, then get the list of snapshot if (string.Compare(vm["ElementName"].ToString(), vmName, true) == 0) { ObjectQuery queryObj1 = new ObjectQuery(string.Format("SELECT * FROM Msvm_VirtualSystemSettingData WHERE SystemName='{0}' and SettingType=5", vm["Name"].ToString())); ManagementObjectSearcher vmSearcher1 = new ManagementObjectSearcher(manScope, queryObj1); ManagementObjectCollection vmCollection1 = vmSearcher1.Get(); ManagementObject snapshot = null; // find and record the snapShot object foreach (ManagementObject snap in vmCollection1) { if (string.Compare(snap["ElementName"].ToString(), snapShotName, true) == 0) { snapshot = snap; break; } } ObjectQuery queryObj2 = new ObjectQuery("SELECT * FROM Msvm_VirtualSystemManagementService"); ManagementObjectSearcher vmSearcher2 = new ManagementObjectSearcher(manScope, queryObj2); ManagementObjectCollection vmCollection2 = vmSearcher2.Get(); ManagementObject virtualSystemService = null; foreach (ManagementObject o in vmCollection2) { virtualSystemService = o; break; } if (ConvertStrToVMState(vm["EnabledState"].ToString()) != VMState.Disabled) { ShutDown(vm["ElementName"].ToString()); } opResult = virtualSystemService.InvokeMethod("ApplyVirtualSystemSnapShot", new object[] { vm.Path, snapshot.Path }); break; } } return "0" == opResult.ToString(); } public static List
最新评论