【问题标题】:C# WMI reading remote event logC# WMI 读取远程事件日志
【发布时间】:2014-05-22 20:28:58
【问题描述】:

我正在尝试对另一台计算机运行 WMI 查询以查找过去 5 小时左右的错误。运行 WMI 查询时,您不应该至少使用 where 子句过滤初始查询吗?

我的代码基于 MSDN 上的 WMI 代码创建器生成的示例

这是我使用的选择查询

    private ManagementScope CreateNewManagementScope(string server)
    {
        string serverString = @"\\" + server + @"\root\cimv2";

        ManagementScope scope = new ManagementScope(serverString);

        return scope;
    } 

            ManagementScope scope = CreateNewManagementScope(servername);
            scope.Connect();
            SelectQuery query = new SelectQuery("select * from Win32_NtLogEvent where TimeWritten > '" + DateTime.Now.AddHours(-5).ToString() + "'");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection logs = searcher.Get();

            int iErrCount = logs.Count;

我只想计算过去 5 小时内的错误数。获取计数时会引发错误。该错误是相当模糊的“通用故障”。

[更新 - 现在使用这样的日期]

                DateTime d = DateTime.UtcNow.AddHours(-12);
            string dateFilter = ManagementDateTimeConverter.ToDmtfDateTime(d);
            SelectQuery query = new SelectQuery("select * from Win32_NtLogEvent where Logfile='Application' AND Type='Error' AND TimeWritten > '" + dateFilter + "'");

使用上面的代码我没有得到任何结果,但我可以在事件日志中看到 2 个错误。日期过滤器有什么问题?

我正在使用这个例子 http://msdn.microsoft.com/en-us/library/system.management.managementdatetimeconverter.todatetime.aspx

【问题讨论】:

  • 代码抛出的错误是什么?
  • “一般故障”。它必须是我如何使用 Win32_NtLogEvent 提供程序,因为我在另一种方法中使用 Win32_Service 提供程序,以相同的方式连接并且工作正常。
  • 这似乎对我有用,虽然看起来 WMI 对日期格式很挑剔,所以结果是错误的。对于日期,我最终使用var d = DateTimeOffset.Now.AddHours(-5); var wmiDate = String.Format("{0:yyyyMMddHHmmss.ffffff}{1}", d, d.Offset.TotalMinutes); 来获取正确格式的日期。也许这会有所帮助。
  • 我发现了一些其他示例,其中提到了 WMI 使用的独特时间格式。所以我现在正在做这件事....但是日期过滤器没有找到任何东西,但我知道我应该找到一些条目。去掉日期过滤器就可以了,DateTime d = DateTime.UtcNow.AddHours(-12);字符串 dateFilter = ManagementDateTimeConverter.ToDmtfDateTime(d); SelectQuery query = new SelectQuery("select * from Win32_NtLogEvent where Logfile='Application' AND Type='Error' AND TimeWritten > '" + dateFilter + "'");

标签: c# wmi wmi-query


【解决方案1】:

我执行了以下操作以使其正常工作。我希望这会有所帮助..

    static void Main(string[] args)
    {
        var conOpt = new ConnectionOptions();
        conOpt.Impersonation = ImpersonationLevel.Impersonate;
        conOpt.EnablePrivileges = true;
        conOpt.Username = "username";
        conOpt.Password = "password";
        conOpt.Authority = string.Format("ntlmdomain:{0}", "yourdomain.com");

        var scope = new 
             ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", 
                                           "yourservername.yourdomain.com"),
                             conOpt);

        scope.Connect();
        bool isConnected = scope.IsConnected;
        if (isConnected)
        {

            /* entire day */ string dateTime = getDmtfFromDateTime(DateTime.Today.Subtract(new TimeSpan(1, 0, 0, 0)));
            string dateTime = getDmtfFromDateTime("09/06/2014 17:00:08"); // DateTime specific

            SelectQuery query = new SelectQuery("Select * from Win32_NTLogEvent Where Logfile = 'Application' and TimeGenerated >='" + dateTime + "'");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection logs = searcher.Get();
            foreach (var log in logs)
            {
                Console.WriteLine("Message : {0}", log["Message"]);
                Console.WriteLine("ComputerName : {0}", log["ComputerName"]);
                Console.WriteLine("Type : {0}", log["Type"]);
                Console.WriteLine("User : {0}", log["User"]);
                Console.WriteLine("EventCode : {0}", log["EventCode"]);
                Console.WriteLine("Category : {0}", log["Category"]);
                Console.WriteLine("SourceName : {0}", log["SourceName"]);
                Console.WriteLine("RecordNumber : {0}", log["RecordNumber"]);
                Console.WriteLine("TimeWritten : {0}", getDateTimeFromDmtfDate(log["TimeWritten"].ToString()));
            }
        }

        //ReadLog();
        Console.ReadLine();
    }

    private static string getDmtfFromDateTime(DateTime dateTime) 
    {
        return ManagementDateTimeConverter.ToDmtfDateTime(dateTime);
    }

    private static string getDmtfFromDateTime(string dateTime)
    {
        DateTime dateTimeValue = Convert.ToDateTime(dateTime);
        return getDmtfFromDateTime(dateTimeValue);
    }

    private static string getDateTimeFromDmtfDate(string dateTime)
    {
        return ManagementDateTimeConverter.ToDateTime(dateTime).ToString();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2013-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多