WMI 实例更新失败
本文关键字:失败 更新 实例 WMI | 更新日期: 2023-09-27 18:33:02
我正在尝试按照 MSDN 中的一个示例使用 C# 更新 WMI 实例,但我无法让它工作。它向我发射了一个"系统.管理.管理异常",它没有给我任何答案。你能告诉我我是否做错了什么吗?
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);
}
我将代码更改为此代码,现在可以工作:
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();
}
}