/// <summary> /// Gets the first virtual machine object of the given class with the given name. /// </summary> /// <param name="name">The name of the virtual machine to retrieve the path for.</param> /// <param name="className">The class of virtual machine to search for.</param> /// <param name="scope">The ManagementScope to use to connect to WMI.</param> /// <returns>The instance representing the virtual machine.</returns> private static ManagementObject GetVmObject(string name, string className, ManagementScope scope) { string vmQueryWql = string.Format(CultureInfo.InvariantCulture, "SELECT * FROM {0} WHERE ElementName=\"{1}\"", className, name); SelectQuery vmQuery = new SelectQuery(vmQueryWql); using (ManagementObjectSearcher vmSearcher = new ManagementObjectSearcher(scope, vmQuery)) using (ManagementObjectCollection vmCollection = vmSearcher.Get()) { if (vmCollection.Count == 0) { throw new ManagementException(string.Format(CultureInfo.CurrentCulture, "No {0} could be found with name \"{1}\"", className, name)); } // // If multiple virtual machines exist with the requested name, return the first // one. // ManagementObject vm = WMIUtils.GetFirstObjectFromCollection(vmCollection); return(vm); } }
/// <summary> /// Gets the virtual machine's configuration settings object. /// </summary> /// <param name="virtualMachine">The virtual machine.</param> /// <returns>The virtual machine's configuration object.</returns> public static ManagementObject GetVirtualMachineSettings(ManagementObject virtualMachine) { using (ManagementObjectCollection settingsCollection = virtualMachine.GetRelated("Msvm_VirtualSystemSettingData", "Msvm_SettingsDefineState", null, null, null, null, false, null)) { ManagementObject virtualMachineSettings = WMIUtils.GetFirstObjectFromCollection(settingsCollection); return(virtualMachineSettings); } }
/// <summary> /// Gets the virtual system snapshot service. /// </summary> /// <param name="scope">The scope to use when connecting to WMI.</param> /// <returns>The virtual system snapshot service.</returns> public static ManagementObject GetVirtualMachineSnapshotService(ManagementScope scope) { using (ManagementClass snapshotServiceClass = new ManagementClass("Msvm_VirtualSystemSnapshotService")) { snapshotServiceClass.Scope = scope; ManagementObject snapshotService = WMIUtils.GetFirstObjectFromCollection(snapshotServiceClass.GetInstances()); return(snapshotService); } }
/// <summary> /// Gets the virtual system management service setting data. /// </summary> /// <param name="scope">The scope to use when connecting to WMI.</param> /// <returns>The virtual system management service settings.</returns> public static ManagementObject GetVirtualMachineManagementServiceSettings(ManagementScope scope) { using (ManagementClass serviceSettingsClass = new ManagementClass("Msvm_VirtualSystemManagementServiceSettingData")) { serviceSettingsClass.Scope = scope; ManagementObject serviceSettings = WMIUtils.GetFirstObjectFromCollection(serviceSettingsClass.GetInstances()); return(serviceSettings); } }