Esempio n. 1
0
        public static int ModifyDevice(this ManagementObject VM, ManagementObject Device, out ManagementObject Job)
        {
            Job = null;
            switch (VM["__CLASS"].ToString().ToUpperInvariant())
            {
            case "MSVM_COMPUTERSYSTEM":
                ManagementObject ServiceObj = GetServiceObject(VM.GetScope(), ServiceNames.VSManagement);
                Job = new ManagementObject();
                return((Int32)ServiceObj.InvokeMethod("ModifyVirtualSystemResources",
                                                      new object[] { VM.Path.Path, Device.GetText(TextFormat.WmiDtd20), null, Job }));

            default:
                return(-1);
            }
        }
Esempio n. 2
0
        public static ManagementObject AddDevice(this ManagementObject VM, ManagementObject Device)
        {
            switch (VM["__CLASS"].ToString().ToUpperInvariant())
            {
            case "MSVM_COMPUTERSYSTEM":
                ManagementObject     ServiceObj = GetServiceObject(VM.GetScope(), ServiceNames.VSManagement);
                ManagementBaseObject inputs     = ServiceObj.GetMethodParameters("AddVirtualSystemResources");
                inputs["TargetSystem"]        = VM.Path.Path;
                inputs["ResourceSettingData"] = new string[] { Device.GetText(TextFormat.WmiDtd20) };
                var result = ServiceObj.InvokeMethod("AddVirtualSystemResources", inputs, null);
                switch (Int32.Parse(result["ReturnValue"].ToString()))
                {
                case (int)ReturnCodes.OK:
                    var tmp = result["NewResources"];
                    return(GetObject(((string[])result["NewResources"]).First()));

                case (int)ReturnCodes.JobStarted:
                    var job = GetObject(result["Job"].ToString());
                    var r   = WaitForJob(job);
                    if (r == 0)
                    {
                        var res  = result["NewResources"];
                        var arr  = (string[])res;
                        var path = arr.First();
                        var o    = GetObject(path);
                        return(GetObject(((string[])result["NewResources"]).First()));
                    }
                    var jobres = job.InvokeMethod("GetError", new ManagementObject(), null);
                    var errStr = jobres["Error"].ToString();
                    var errObj = GetObject(errStr);
                    return(null);

                default:
                    return(null);
                }

            default:
                return(null);
            }
        }
Esempio n. 3
0
        /// <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);
            }
        }
Esempio n. 4
0
 public static ManagementObject NewResource(this ManagementObject VM, ResourceTypes ResType, string SubType, string Server = null)
 {
     return(NewResource(VM, ResType, SubType, Server == null?VM.GetScope():GetScope(Server)));
 }