简体   繁体   中英

Simple C# WMI Get & Put

I am trying to Read & Put values from and to WMI using C#.

The current example uses ccm namespace, for configmgr client.

The read functions works correctly, able to read ADV_RepeatRunBehavior value. Though the Put(); doesn't work as expected, the values are not stored back and Invalid Class exception is thrown.

Some advice would be nice as I am new to this, many thanks.

static void Main(string[] args)
{
    try
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(
            "root\\ccm\\Policy\\Machine", 
            "SELECT * FROM CCM_SoftwareDistribution WHERE PKG_PackageID='XXXXXXXX'");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            //Read works
            //Console.WriteLine(queryObj["ADV_RepeatRunBehavior"].ToString());
            //Console.ReadLine();

            //Put doesn't
            queryObj["ADV_RepeatRunBehavior"] = "RerunNever";
            queryObj.Put();
        }
    }
    catch (ManagementException z)
    {
        Console.WriteLine("An error occurred: " + z.Message);
        Console.ReadLine();
    }
}

Found the resolution for this.

  1. You have to run Visual Studio as Administrator if testing on localhost
  2. The connection to WMI has to be \\\\root\\\\ccm\\\\Policy\\\\Machine\\\\ActualConfig then its possible to Put() values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM