1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
//read eventlogs from system part of a remote machine or list of remote machines and write at a csv at will
using System;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
class MyEventLogClass
{
public static void Main(string[] Arg)
{
string[] myHostName= new String [10000];
string myEventType= null;
string strLogFile= "NoLog";
int myArgLength=0;
int myEventID=-1;
//check arguments
//nothing means user will be asked for hostname, Event Type and ID
//one parameter it should be HOSTNAMELISTFILE as Textlist but ask for EventType and ID
//three parameters should be HOSTNAMELISTFILE EventType EventID
//four parameters should be HOSTNAMELISTFILE EventType EventID LogFile
try
{
myArgLength= Arg.Length;
switch(myArgLength)
{
case 0 :
myHostName[0] = MyEventLogClass.getHostname();
break;
case 1 :
myHostName = MyEventLogClass.getHostNameList(Arg[0]);
break;
case 3 :
myHostName = MyEventLogClass.getHostNameList(Arg[0]);
myEventType = Arg[1];
myEventID = int.Parse(Arg[2]);
break;
case 4 :
myHostName = MyEventLogClass.getHostNameList(Arg[0]);
myEventType = Arg[1];
myEventID = int.Parse(Arg[2]);
strLogFile = Arg[3];
break;
default:
MyEventLogClass.writeHelp();
break;
}
}
catch
{
myHostName[0] = MyEventLogClass.getHostname();
}
//get EventType and ID if not set
if ((myEventType == "") || (myEventType == null))
{
myEventType = MyEventLogClass.getEventType();
}
if (myEventID < 0)
{
myEventID = MyEventLogClass.getEventID();
}
//if LogFile wanted prepare header in csv style
if (!(strLogFile.Equals("NoLog")))
{
try
{
File.WriteAllText(strLogFile, "EventID, EntryType, UserName, Source, MachineName, Index, Data, Message\n");
}
catch
{
Console.WriteLine("Failure during access to {0}", strLogFile);
Environment.Exit (-1);
}
}
//Call Eventsearch for each given Hostname
foreach(string strHost in myHostName)
{
if(( strHost != "") || ( strHost != "#") || ( strHost != null))
{
MyEventLogClass.getEvent(strHost, myEventType, myEventID, strLogFile);
}
}
Console.WriteLine("\n\nEventlog scan done with these Parameters");
Console.WriteLine("Hostname \t{0}", myHostName);
Console.WriteLine("EventType \t{0}", myEventType);
Console.WriteLine("EventID \t{0}", myEventID);
Console.WriteLine("LogFile \t{0}", strLogFile);
//end of Main
}
private static void getEvent(string myHostName, string myEventType, int myEventID, string strLogFile)
{
int myCount=0;
string mystrLog= null;
//create a regular expression that matches one or more Line breaks
Regex rgx = new Regex("[\n\b\r]");
// catch if myHostName is nothing realy
if(( myHostName == "") || ( myHostName == "#") || ( myHostName == null))
{
return;
}
Console.WriteLine("\nSearching " + myHostName + " " + myEventType + " " + myEventID + "\n");
// Associate the instance of 'EventLog' myHostName's System Log
EventLog myEventLog = new EventLog("System", ".");
myEventLog.MachineName = myHostName;
// Get Object Instance EventLog Collection
EventLogEntryCollection myLogEntryCollection=myEventLog.Entries;
myCount =myLogEntryCollection.Count;
// Iterate through all 'EventLogEntry' instances in 'EventLog'.
for(int i=myCount-1;i>0;i--)
{
EventLogEntry myLogEntry = myLogEntryCollection[i];
// get the entries having desired EventType AND EventID
if(((myLogEntry.EntryType.ToString().Equals(myEventType))) && myLogEntry.EventID.Equals(myEventID))
{
// Display Source of the event.
Console.WriteLine("Data {0}", myLogEntry.Data);
Console.WriteLine("EntryType {0}", myLogEntry.EntryType);
Console.WriteLine("EventID {0}", myLogEntry.EventID);
Console.WriteLine("Index {0}", myLogEntry.Index);
Console.WriteLine("MachineName {0}", myLogEntry.MachineName);
Console.WriteLine("Source {0}", myLogEntry.Source);
Console.WriteLine("TimeGenerated {0}", myLogEntry.TimeGenerated);
Console.WriteLine("Type {0}", myLogEntry.EntryType);
Console.WriteLine("UserName {0}", myLogEntry.UserName);
Console.WriteLine("Messages {0}", (rgx.Replace(myLogEntry.Message, "")));
//write Log if wanted to
if (!(strLogFile.Equals("NoLog")))
{
mystrLog= (myLogEntry.EventID + "," +
myLogEntry.EntryType + "," +
myLogEntry.UserName + "," +
myLogEntry.Source + "," +
myLogEntry.MachineName + "," +
myLogEntry.Index + "," +
myLogEntry.Data + "," +
(rgx.Replace(myLogEntry.Message, "")));
try
{
using (StreamWriter sWriter= File.AppendText(strLogFile))
{
sWriter.WriteLine(mystrLog);
}
}
catch
{
Console.WriteLine("Failure during access to {0}", strLogFile);
Environment.Exit (-1);
}
}
//take out the remark to get only the Last entry
//return;
}
}
}
private static string[] getHostNameList(string myListFileName)
{
// write Help if has been asked for
if ((myListFileName.Equals("?"))||(myListFileName.Equals("/?")))
{
MyEventLogClass.writeHelp();
}
//Exit Program if Hostname file not found
if (!File.Exists(myListFileName))
{
Console.WriteLine("File {0} not found", myListFileName);
Environment.Exit (-1);
}
string[] myEntry= new String[10000];
myEntry = File.ReadAllLines(myListFileName);
return myEntry;
}
private static string getHostname()
{
String myHostName=System.Environment.MachineName;
// get search parameters Hostname,EventType and EventID
Console.WriteLine("\n Please Enter Hostname");
Console.WriteLine("Default {0}", myHostName);
try
{
myHostName=Convert.ToString(Console.ReadLine());
}
catch(Exception)
{
myHostName =System.Environment.MachineName;
}
if (myHostName.Equals(""))
{
myHostName =System.Environment.MachineName;
}
return myHostName;
}
private static string getEventType()
{
String myEventType=null;
int myOption=0;
// get EvenType
Console.WriteLine("\n Select the Event Type");
Console.WriteLine("(1):Error 2:Information 3:Warning");
// get user choice
try
{
myOption=Convert.ToInt16(Console.ReadLine());
}
catch(Exception)
{
myOption=1;
}
switch(myOption)
{
case 1: myEventType="Error";
break;
case 2: myEventType="Information";
break;
case 3: myEventType="Warning";
break;
default: myEventType="Error";
break;
}
return myEventType;
}
private static int getEventID()
{
int myEventID=0;
// get EventID
Console.WriteLine("\n Select the Event ID Number default is 0");
try
{
myEventID=Convert.ToInt16(Console.ReadLine());
}
catch(Exception)
{
myEventID=0;
}
return myEventID;
}
private static void writeHelp()
{
Console.WriteLine("\nEventlog.exe is a .Net application to collect event entries from system log");
Console.WriteLine("\nParameters Can be ? or HOSTNAMEFILE and EventType + EventID");
Console.WriteLine("\nParameters:");
Console.WriteLine("Eventlog HOSTNAMEFILE");
Console.WriteLine("Eventlog HOSTNAMEFILE EventType EventID");
Console.WriteLine("Eventlog HOSTNAMEFILE EventType EventID LOGFILENAME");
Console.WriteLine("Example for a search of DCOM caused 100016 Errors:");
Console.WriteLine("Eventlog Hostnames.txt Error 100016 DCOM_Errors.csv");
Environment.Exit (-1);
}
}
|