0

I'm trying to update a WMI instance using C# following one example in MSDN but I cannot get it to work. It is firing me a 'System.Management.ManagementException' which does not give me any answer. Can you please tell me if I'm doing something wrong?

    public void UpdateInstance(string parametersJSON)
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        object result = serializer.Deserialize(parametersJSON, typeof(object));
        Dictionary<string, object> dic = (Dictionary<string, object>)result;

        PutOptions options = new PutOptions();
        options.Type = PutType.UpdateOnly;

        ManagementObject objHostSetting = new ManagementObject();
        objHostSetting.Scope = new ManagementScope("root\\onguard");
        objHostSetting.Path = new ManagementPath("Lnl_Cardholder.SSNO = '33263085'"); // This is the line that fires the exception

        foreach (KeyValuePair<string, object> value in dic)
        {
            objHostSetting[value.Key] = value.Value.ToString();
        }

        //update the ManagementObject
        objHostSetting.Put(options);
    }
NicoRiff
  • 4,803
  • 3
  • 25
  • 54

1 Answers1

0

I changed my code to this and now works:

public void UpdateInstance(string scope, string query, string parametersJSON)
        {
            WindowsImpersonationContext impersonatedUser = WindowsIdentity.GetCurrent().Impersonate();

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            object result = serializer.Deserialize(parametersJSON, typeof(object));

            Dictionary<string, object> dic = (Dictionary<string, object>)result;

            ManagementObjectSearcher searcher;
            searcher = new ManagementObjectSearcher(scope, query);

            EnumerationOptions options = new EnumerationOptions();
            options.ReturnImmediately = true;

            foreach (ManagementObject m in searcher.Get())
            {
                foreach (KeyValuePair<string, object> k in dic)
                {
                    m.Properties[k.Key].Value = k.Value;
                }

                m.Put();
            }
        }
NicoRiff
  • 4,803
  • 3
  • 25
  • 54