开发者

query on Win32_NTLogEvent WHERE Logfile = 'Security' works only on remote machine

开发者 https://www.devze.com 2023-01-30 00:12 出处:网络
I\'ve have a problem using the code below to retrieve data from the security log event of my local machine. I tested on various computers: the local machine is a windows xp sp3. The query has no error

I've have a problem using the code below to retrieve data from the security log event of my local machine. I tested on various computers: the local machine is a windows xp sp3. The query has no error but it returns 0 record. For remote machines it works perfectly Anyone can give me a solution? This is the code:

using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                string[] arrComputers = {".","clientN"};
                foreach (string strComputer in arrComputers)
                {
                    Console.WriteLine("==========================================");
                    Console.WriteLine("Computer: " + strComputer);
                    Console.WriteLine("==========================================");

                    ManagementObjectSearcher searcher = 
                        new ManagementObjectSearcher(
                        "\\\\" + strComputer + "\\root\\CIMV2", 
                        "SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Security'"); 

                    foreach (ManagementObject queryObj in searcher.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Win32_NTLogEvent instance");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("RecordNumber: {0}", queryObj["RecordNumber"]);
                        Console.WriteLine("SourceName: {0}", queryObj["SourceName"]);
                        Console.WriteLine("TimeGenerated: {0}", queryObj["TimeGenerated"]);
                    }
                }开发者_Go百科
            }
            catch(ManagementException err)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + err.Message);
            }
        }
    }
}

I understood that using the impersonation level for the wmi query in vbs it works.

    Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate,(Security)}!\\" _
& strComputer & "\root\cimv2")
Set colLoggedEvents = objWMI.ExecQuery _
("Select * from Win32_NTLogEvent Where Logfile = 'Security'" )

So I have to translate in c#.


Ok so I close this Question using my code. the code is:

     using System; 
        using System.Management; 
        namespace WMISample 
        {  
            public class MyWMIQuery 
            { 
                public static void Main() 
                { 
                    try 
                    { 
                        ConnectionOptions oConn = new ConnectionOptions();
                        oConn.Impersonation = ImpersonationLevel.Impersonate;
                        oConn.EnablePrivileges = true;

                        string[] arrComputers = {".","clientN"}; 
                        foreach (string strComputer in arrComputers) 
                        { 
                            Console.WriteLine("=========================================="); 
                            Console.WriteLine("Computer: " + strComputer); 
                            Console.WriteLine("=========================================="); 
                            ManagementObjectSearcher searcher = new ManagementObjectSearcher
                            (
                               new ManagementScope("\\\\" + strComputer + "\\root\\CIMV2",  oConn),
                               new ObjectQuery( @"SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Security'")
                            );


                            foreach (ManagementObject queryObj in searcher.Get()) 
                            { 
                                Console.WriteLine("-----------------------------------"); 
                                Console.WriteLine("Win32_NTLogEvent instance"); 
                                Console.WriteLine("-----------------------------------"); 
                                Console.WriteLine("RecordNumber: {0}", queryObj["RecordNumber"]); 
                                Console.WriteLine("SourceName: {0}", queryObj["SourceName"]); 
                                Console.WriteLine("TimeGenerated: {0}", queryObj["TimeGenerated"]); 
                            } 
                        } 
                    } 
                    catch(ManagementException err) 
                    { 
                        MessageBox.Show("An error occurred while querying for WMI data: " + err.Message); 
                    } 
                } 
            } 
        } 


try using the local computername instead of ".". so, instead of

string[] arrComputers = {".","clientN"};

you would have

string[] arrComputers = { Environment.GetEnvironmentVariable("computername"), "clientN" };

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号