WMI

T

Troy

Hello,

I'm just starting out with c# and WMI and have a few questions that seasoned
coders might be able to help me with. I'd like to write a console app that
would query a server and pull out all the errors in the system and
application log. I've started writing the class to do this but i'm stuck now
because I have no idea on how to do this... here's my code... any help is
appreciated.

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

#endregion

namespace EventlogWatcher
{
public class EventLog
{
// variables to store server name and event log type
private string serverName, logType;
private const string user="softlnding";
private const string password = "$0ftLand1ng";


public EventLog(string serverName, string logType)
{
this.logType = logType;
this.serverName = serverName;
}

private void GetLogs()
{
//Build an options object for the connection
ConnectionOptions options = new ConnectionOptions();
options.Username = user;
options.Password = password;

//Make a connection to a remote computer using these options
ManagementScope scope = new
ManagementScope("\\\\Server1\\root\\cimv2", options);
scope.Connect();

SelectQuery query = new SelectQuery("Select * from
Win32_NTEventLogFile Where LogFileName='Application'");


}

}
}
 
D

Dale Preston

You have done a great job on investigating the WMI connection requirements
but it isn't necessary for reading the event logs of a remote machine.
Here's how to do it. The for loop displays all of the entries for the last
24 hours from newest to oldest.

// Create the event log object
eventLog = new System.Diagnostics.EventLog();
eventLog.MachineName = serverName;
eventLog.Log = "System"; // or "Application" or any other log you want to
view

for(int x = eventLog.Entries.Count - 1; x >= 0; x--)
{
EventLogEntry entry = eventLog.Entries[x];
if (entry.TimeGenerated < DateTime.Now.AddDays(-1))
{
break;
}
}

HTH

Dale Preston
MCAD, MCDBA, MCSE
 
R

_R

Here's how to do it. The for loop displays all of the entries for the last
24 hours from newest to oldest.

// Create the event log object
eventLog = new System.Diagnostics.EventLog();
eventLog.MachineName = serverName;
eventLog.Log = "System"; // or "Application" or any other log you want to
view

for(int x = eventLog.Entries.Count - 1; x >= 0; x--)
{
EventLogEntry entry = eventLog.Entries[x];
if (entry.TimeGenerated < DateTime.Now.AddDays(-1))
{
break;
}
}

HTH

Doesn't this generate an access violation if you attempt to read
another (networked) machine's event queue?
 
D

Dale Preston

Not if you're a domain administrator.

Dale

_R said:
Here's how to do it. The for loop displays all of the entries for the last
24 hours from newest to oldest.

// Create the event log object
eventLog = new System.Diagnostics.EventLog();
eventLog.MachineName = serverName;
eventLog.Log = "System"; // or "Application" or any other log you want to
view

for(int x = eventLog.Entries.Count - 1; x >= 0; x--)
{
EventLogEntry entry = eventLog.Entries[x];
if (entry.TimeGenerated < DateTime.Now.AddDays(-1))
{
break;
}
}

HTH

Doesn't this generate an access violation if you attempt to read
another (networked) machine's event queue?
 
T

Troy

dude you rock....

Dale Preston said:
Not if you're a domain administrator.

Dale

want
to
view

for(int x = eventLog.Entries.Count - 1; x >= 0; x--)
{
EventLogEntry entry = eventLog.Entries[x];
if (entry.TimeGenerated < DateTime.Now.AddDays(-1))
{
break;
}
}

HTH

Doesn't this generate an access violation if you attempt to read
another (networked) machine's event queue?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top