public static ManagementObject NewVM(string VMName, string Server = null) { if (VMName == null) return null; var MgmtSvc = GetServiceObject(GetScope(Server), ServiceNames.VSManagement); var settings = new ManagementClass(GetScope(Server), new ManagementPath(VMStrings.GlobalSettingData), new ObjectGetOptions()).CreateInstance(); if (settings == null) return null; settings["ElementName"] = VMName; ManagementBaseObject inputs = MgmtSvc.GetMethodParameters("DefineVirtualSystem"); inputs["SystemSettingData"] = settings.GetText(TextFormat.WmiDtd20); inputs["ResourceSettingData"] = null; inputs["SourceSetting"] = null; //var input = new object[] {settings.GetText(TextFormat.WmiDtd20), null, null, Comp, Job}; var result = MgmtSvc.InvokeMethod("DefineVirtualSystem", inputs, null); switch (Int32.Parse(result["ReturnValue"].ToString())) { case (int)ReturnCodes.OK: return GetObject(result["DefinedSystem"].ToString()); case (int)ReturnCodes.JobStarted: return WaitForJob((ManagementObject)result["Job"]) == 0 ? GetObject(result["DefinedSystem"].ToString()) : null; default: return null; } }
private static void ReplaceClassIfNecessary(string classPath, ManagementClass newClass) { try { ManagementClass class2 = SafeGetClass(classPath); if (class2 == null) { newClass.Put(); } else if (newClass.GetText(TextFormat.Mof) != class2.GetText(TextFormat.Mof)) { class2.Delete(); newClass.Put(); } } catch (ManagementException exception) { throw new ArgumentException(string.Format(RC.GetString("CLASS_NOTREPLACED_EXCEPT") + "\r\n{0}\r\n{1}", classPath, newClass.GetText(TextFormat.Mof)), exception); } }
private static void ReplaceClassIfNecessary(string classPath, ManagementClass newClass) { try { ManagementClass managementClass = SchemaNaming.SafeGetClass(classPath); if (managementClass != null) { if (newClass.GetText(TextFormat.Mof) != managementClass.GetText(TextFormat.Mof)) { managementClass.Delete(); newClass.Put(); } } else { newClass.Put(); } } catch (ManagementException managementException1) { ManagementException managementException = managementException1; string str = string.Concat(RC.GetString("CLASS_NOTREPLACED_EXCEPT"), "\r\n{0}\r\n{1}"); throw new ArgumentException(string.Format(str, classPath, newClass.GetText(TextFormat.Mof)), managementException); } }
/// <summary> /// Given a class path, and a ManagementClass class definition, this /// function will create the class if it does not exist, replace the /// class if it exists but is different, or do nothing if the class /// exists and is identical. This is useful for performance reasons /// since it can be expensive to delete an existing class and replace /// it. /// </summary> /// <param name="classPath">WMI path to class</param> /// <param name="newClass">Class to create or replace</param> static void ReplaceClassIfNecessary(string classPath, ManagementClass newClass) { try { ManagementClass oldClass = SafeGetClass(classPath); if(null == oldClass) newClass.Put(); else { // if(newClass.GetText(TextFormat.Mof) != oldClass.GetText(TextFormat.Mof)) { // oldClass.Delete(); newClass.Put(); } } } catch(ManagementException e) { string strformat = RC.GetString("CLASS_NOTREPLACED_EXCEPT") + "\r\n{0}\r\n{1}"; throw new ArgumentException(String.Format(strformat, classPath, newClass.GetText(TextFormat.Mof)), e); } }