public static IEnumerable <ManagementObject> GetDevices(this ManagementObject VM) { switch (VM["__CLASS"].ToString().ToUpperInvariant()) { case "MSVM_COMPUTERSYSTEM": return (VM.GetSettings() .GetRelated(VMStrings.ResAllocData) .Cast <ManagementObject>()); case "MSVM_VIRTUALSYSTEMSETTINGDATA": return (VM.GetRelated(VMStrings.ResAllocData) .Cast <ManagementObject>()); default: break; } return(null); }
/// <summary> /// Modifies the CPU and/or Memory configuration of a Virtual Machine. /// </summary> /// <param name="VM">The Virtual Machine to modify.</param> /// <param name="ProcCount">Number of processors to allocate. 'null' to leave unchanged.</param> /// <param name="ProcLimit">Percent limit of processor usage to allow. Range: 1 to 1000. 'null' to leave unchanged.</param> /// <param name="Memory">Amount of initial RAM (in MB) to allocate. 'null' to leave unchanged.</param> /// <param name="MemoryLimit">Maximum RAM that may be allocated when using Dynamic Memory. Only applicable if Dynamic Memory is enabled. 'null' to leave unchanged.</param> /// <param name="EnableDynamicMemory">Enables or disables Dynamic Memory on this VM. 'null' to leave unchanged.</param> /// <returns>True if a change was succesfully made.</returns> public static bool ModifyVMConfig(this ManagementObject VM, int?ProcCount = null, int?ProcLimit = null, int?Memory = null, int?MemoryLimit = null, bool?EnableDynamicMemory = null) { // Do we have inputs? if (ProcCount == null && ProcLimit == null && Memory == null && MemoryLimit == null && EnableDynamicMemory == null) { return(false); } // Rough verify of inputs if (VM == null || !VM["__CLASS"].ToString().Equals(VMStrings.ComputerSystem, StringComparison.InvariantCultureIgnoreCase)) { throw new ArgumentException(); } if ((ProcCount != null && (ProcCount < 1 || ProcCount > 4)) || (ProcLimit != null && (ProcLimit < 1 || ProcLimit > 1000)) || (Memory != null && Memory < 1) || (MemoryLimit != null && ((Memory != null && MemoryLimit < Memory) || MemoryLimit < 1))) { throw new ArgumentOutOfRangeException(); } ManagementScope scope = VM.GetScope(); ManagementObject VMSettings = VM.GetSettings(); List <string> NewSettings = new List <string>(); if (ProcCount != null || ProcLimit != null) { var set = VMSettings.GetRelated(VMStrings.ProcessorSettings).Cast <ManagementObject>().First(); if (ProcCount != null) { set["VirtualQuantity"] = ProcCount; } if (ProcLimit != null) { set["Limit"] = (ProcLimit * 100); // Base unit is %0.001, but I'm only operating in tenths of a percent. } NewSettings.Add(set.GetText(TextFormat.WmiDtd20)); } if (Memory != null || MemoryLimit != null || EnableDynamicMemory != null) { var set = VMSettings.GetRelated(VMStrings.MemorySettings).Cast <ManagementObject>().First(); if (EnableDynamicMemory != null) { set["DynamicMemoryEnabled"] = EnableDynamicMemory; } if (MemoryLimit != null) { if ((bool)set["DynamicMemoryEnabled"]) { set["Limit"] = MemoryLimit; if ((int)set["VirtualQuantity"] > MemoryLimit) { set["VirtualQuantity"] = MemoryLimit; } } else if (Memory == null) { set["VirtualQuantity"] = MemoryLimit; } } if (Memory != null) { set["VirtualQuantity"] = Memory; set["Reservation"] = Memory; if ((bool)set["DynamicMemoryEnabled"]) { set["Limit"] = Memory; } } NewSettings.Add(set.GetText(TextFormat.WmiDtd20)); } ManagementObject Job = new ManagementObject(); var MgmtSvc = GetServiceObject(scope, ServiceNames.VSManagement); uint ret = (uint)MgmtSvc.InvokeMethod("ModifyVirtualSystemResources", new object[] { VM, NewSettings.ToArray(), Job }); switch (ret) { case (int)ReturnCodes.OK: return(true); case (int)ReturnCodes.JobStarted: return(WaitForJob(Job) == 0); default: return(false); } }