Пример #1
0
        /// <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);
            }
        }
 /// <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
         {
             // TODO: Figure Out Why CompareTo does not work!!!
             //                if(false == newClass.CompareTo(newClass, ComparisonSettings.IgnoreCase | ComparisonSettings.IgnoreObjectSource))
             if (newClass.GetText(TextFormat.Mof) != oldClass.GetText(TextFormat.Mof))
             {
                 // TODO: Log to context?
                 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);
     }
 }
    static void Test()
    {
        ManagementClass newClass = new ManagementClass("root", "", null);

        newClass.SystemProperties ["__CLASS"].Value = "xyz";

        ManagementClass paramsIn = new ManagementClass("root:__PARAMETERS");

        paramsIn.Properties.Add("param1", CimType.String, false);
        paramsIn.Properties["param1"].Qualifiers.Add("IN", true);
        paramsIn.Properties["param1"].Qualifiers.Add("ID", 0);
        paramsIn.Properties.Add("param2", CimType.String, false);
        paramsIn.Properties["param2"].Qualifiers.Add("IN", true);
        paramsIn.Properties["param2"].Qualifiers.Add("ID", 1);

        ManagementClass paramsOut = new ManagementClass("root:__PARAMETERS");

        paramsOut.Properties.Add("param1", CimType.String, false);
        paramsOut.Properties["param1"].Qualifiers.Add("OUT", true);
        paramsOut.Properties["param1"].Qualifiers.Add("ID", 0);
        paramsOut.Properties.Add("ReturnValue", CimType.String, false);
        paramsOut.Properties["ReturnValue"].Qualifiers.Add("OUT", true);


        newClass.Methods.Add("F1", paramsIn, paramsOut);
        Console.WriteLine(newClass.GetText(TextFormat.Mof));
    }
Пример #4
0
    static void Mainx()
    {
        // Get a ManagementClass and ManagementObject to serialize
        ManagementClass  cls = new ManagementClass("__Win32Provider");
        ManagementObject obj = new ManagementObject("root:__Namespace='cimv2'");

        // Show the info we want to serialize
        Console.WriteLine("*** INITIAL CLASS PROPERTIES AND VALUES");
        foreach (PropertyData prop in cls.Properties)
        {
            Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
        }
        Console.WriteLine("*** INITIAL CLASS MOF");
        Console.WriteLine(cls.GetText(TextFormat.Mof));
        Console.WriteLine("*** INITIAL OBJECT PROPERTIES AND VALUES");
        foreach (PropertyData prop in obj.Properties)
        {
            Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
        }
        Console.WriteLine("*** INITIAL OBJECT MOF");
        Console.WriteLine(obj.GetText(TextFormat.Mof));

        //Open a file and serialize a ManagementClass and a ManagementObject into the file
        Stream          stream     = File.Open("MyClass1MyClass2.bin", FileMode.Create);
        BinaryFormatter bformatter = new BinaryFormatter();

        bformatter.Serialize(stream, cls);
        bformatter.Serialize(stream, obj);
        stream.Close();

        //Open file "MyClass1MyClass2.bin" and deserialize the MyClass1 object from it.
        stream     = File.Open("MyClass1MyClass2.bin", FileMode.Open);
        bformatter = new BinaryFormatter();
        ManagementBaseObject baseObject1 = (ManagementBaseObject)bformatter.Deserialize(stream);
        ManagementBaseObject baseObject2 = (ManagementBaseObject)bformatter.Deserialize(stream);

        stream.Close();

        // Show the Deserialized information
        Console.WriteLine("*** DESERIALIZED CLASS PROPERTIES AND VALUES");
        foreach (PropertyData prop in baseObject1.Properties)
        {
            Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
        }
        Console.WriteLine("*** DESERIALIZED CLASS MOF");
        Console.WriteLine(baseObject1.GetText(TextFormat.Mof));
        Console.WriteLine("*** DESERIALIZED OBJECT PROPERTIES AND VALUES");
        foreach (PropertyData prop in baseObject2.Properties)
        {
            Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
        }
        Console.WriteLine("*** DESERIALIZED OBJECT MOF");
        Console.WriteLine(baseObject2.GetText(TextFormat.Mof));
    }
Пример #5
0
    public static void Main()
    {
        // Get the WMI class
        ManagementClass processClass =
            new ManagementClass("Win32_Process");

        processClass.Options.UseAmendedQualifiers = true;

        Console.WriteLine(
            processClass.GetText(
                TextFormat.Mof));
    }
Пример #6
0
        /// <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)
        {
            ManagementClass oldClass = SafeGetClass(classPath);

            if (null == oldClass)
            {
                newClass.Put();
            }
            else
            {
                // TODO: Figure Out Why CompareTo does not work!!!
                //                if(false == newClass.CompareTo(newClass, ComparisonSettings.IgnoreCase | ComparisonSettings.IgnoreObjectSource))
                if (newClass.GetText(TextFormat.Mof) != oldClass.GetText(TextFormat.Mof))
                {
                    // TODO: Log to context?
                    oldClass.Delete();
                    newClass.Put();
                }
            }
        }
 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);
     }
 }
Пример #8
0
        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);
            }
        }