分配CIM_DATETIME时WMI类型不匹配

分配CIM_DATETIME时WMI类型不匹配

问题描述:

我认为我遇到的问题是CIM_DATETIME和DateTime之间的类型不匹配。分配CIM_DATETIME时WMI类型不匹配

下面是泛型服务器类中的一个函数。在调用这个函数的时候,我已经创建了一个ManagementScope对象,并通过测试一个WMI查询来验证连接。

public void Sync() 
{ 
    ManagementPath path = new ManagementPath("[email protected]"); 
    ManagementObject classInstance = new ManagementObject(this.Scope, path, null); 
    ManagementBaseObject inParams = classInstance.GetMethodParameters("SetDateTime"); 
    inParams["LocalDateTime"] = "20170811101838.000000+000"; 
    ManagementBaseObject outParams = classInstance.InvokeMethod("SetDateTime", inParams, null); 
    Console.WriteLine(outParams["ReturnValue"]); 
} 

只要我尝试为“LocalDateTime”属性赋值,就会遇到Type Mismatch异常。

如果我尝试获取inParams["LocalDateTime"]的类型,将导致Null引用异常。这导致我相信LocalDateTime不是一个有效的索引,或者它只是引用一个空对象。

但是,我可以通过枚举其属性来看到LocalDateTime是inParams的一个属性。

foreach (var prop in inParams.Properties) 
{ 
    Console.WriteLine($"IsArray : {inParams.IsArray}"); 
    Console.WriteLine($"IsLocal : {inParams.IsLocal}"); 
    Console.WriteLine($"Name : {inParams.Name}"); 
    Console.WriteLine($"Origin : {inParams.Origin}"); 
    Console.WriteLine($"Type : {inParams.Type}"); 
    Console.WriteLine($"Value : {inParams.Value}"); 
} 

这将返回以下,

IsArray : False 
IsLocal : False 
Name : LocalDateTime 
Origin : __PARAMETERS 
Type : DateTime 
Value : 

所以,我的问题是

  1. 如何正确引用类Win32_OperatingSystemSetDateTime方法的LocalDateTime参数?

  2. 是否可以投射到CimType.DateTime

如果有人在将来在此跌倒...

我需要为System.DateTime转换为使用ManagementDateTimeConverter.ToDmtfDateTime方法DMTF日期时间。

inParams["LocalDateTime"] = ManagementDateTimeConverter.ToDmtfDateTime(DateTime.Now);

为了走另一条路,从DMTF日期时间为System.DateTime使用ManagementDateTimeConverter.ToDateTime方法需要。

var systemDateTime = ManagementDateTimeConverter.ToDateTime("20170811101838.000000+000");