ans
ans

Reputation: 33

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();
    }
}

Upvotes: 1

Views: 1336

Answers (1)

ans
ans

Reputation: 33

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.

Upvotes: 1

Related Questions