使用 C# .net 运行 WMI 查询
关于为什么在多次运行查询时不会总是返回时间范围内的所有相同事件并且在这样做时也不会引发异常的任何想法?
ConnectionOptions opts = new ConnectionOptions();
if (EncryptConnections)
{
opts.Authentication = AuthenticationLevel.PacketPrivacy;
}
opts.Username = eventSource.user;
opts.SecurePassword = eventSource.password;
opts.Impersonation = ImpersonationLevel.Impersonate;
string location = string.Format("\\\\{0}\\root\\cimv2", eventSource.machine);
ManagementScope scope = new ManagementScope(location, opts);
scope.Connect();
EnumerationOptions enumOptions = new EnumerationOptions();
enumOptions.DirectRead = false;
enumOptions.EnumerateDeep = false;
enumOptions.ReturnImmediately = true;
enumOptions.Rewindable = false;
enumOptions.Timeout = TimeSpan.MaxValue;
enumOptions.BlockSize = 10;
WqlObjectQuery query = new WqlObjectQuery("Select * from Win32_NTLogEvent Where Logfile = 'Security' AND TimeWritten >= '20110614025212.000000+000' AND TimeWritten <= '20110614030712.000000+000'");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query, enumOptions);
foreach (ManagementBaseObject mbo in searcher.Get() ) {
// do Stuff
}
在实际代码中,查询每次都会更改以获得不同的时间范围,但不完整的结果会显示不完整的结果。
每次运行时,ManagementObjectCollection
返回的 from都会searcher.Get()
成功枚举列表 - 没有引发异常,但是集合并不总是相同的。
每次都可能对集合进行不同的排序,这是意料之中的。该集合并不总是包含相同时间范围内的相同事件计数,这是意料之外的。
每隔几百个查询,该集合似乎就无法获得完整的 WMI 结果。它默默地这样做,我还没有发现任何异常或错误消息。
我们发现,对于某些日志文件类型(至少值得注意的是安全性),“逻辑”运算符<=
和<
反对时间的行为也不符合预期,因此我们已经必须在每一端使用包含的 <= 来处理重叠的结束时间点。
上述丢失结果的问题不是由于逻辑运算符未能包含 == 的时间。