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
		private static string GetMofFormat(ManagementObject obj)
		{
			return string.Concat(obj.GetText(TextFormat.Mof).Replace("\n", "\r\n"), "\r\n");
		}
Esempio n. 4
0
        private ManagementObject AddVirtualMachineResources(ManagementObject objVm, ManagementObject resource)
        {
            if (resource == null)
                return resource;

            // request management service
            ManagementObject objVmsvc = GetVirtualSystemManagementService();

            // add resources
            string txtResource = resource.GetText(TextFormat.CimDtd20);
            ManagementBaseObject inParams = objVmsvc.GetMethodParameters("AddVirtualSystemResources");
            inParams["TargetSystem"] = objVm;
            inParams["ResourceSettingData"] = new string[] { txtResource };
            ManagementBaseObject outParams = objVmsvc.InvokeMethod("AddVirtualSystemResources", inParams, null);
            JobResult result = CreateJobResultFromWmiMethodResults(outParams);

            if (result.ReturnValue == ReturnCode.OK)
            {
                string[] wmiPaths = (string[])outParams["NewResources"];
                return wmi.GetWmiObjectByPath(wmiPaths[0]);
            }
            else if (result.ReturnValue == ReturnCode.JobStarted)
            {
                if (JobCompleted(result.Job))
                {
                    string[] wmiPaths = (string[])outParams["NewResources"];
                    return wmi.GetWmiObjectByPath(wmiPaths[0]);
                }
                else
                {
                    throw new Exception("Cannot add virtual machine resources");
                }
            }
            else
            {
                throw new Exception("Cannot add virtual machine resources: " + txtResource);
            }
        }
Esempio n. 5
0
 static string GetMofFormat(ManagementObject obj)
 {
     return obj.GetText(TextFormat.Mof).Replace("\n", "\r\n") + "\r\n";
 }