I wrote this for my MobileExec Program to show the most recent events in the event log. You can see the results in the screenshot above. The WMI SQL query gets kind of cryptic, but when implemented can be quite powerful. Check out the code below:
Label1.ForeColor = Color.Black;
Label1.Text = "Sorry! No Logs Found by that description!";
ConnectionOptions co = new ConnectionOptions();
co.Username = txtUsername.Text;
co.Password = txtPassword.Text;
co.Impersonation = ImpersonationLevel.Impersonate;
co.EnablePrivileges = true;
co.Authentication = AuthenticationLevel.PacketPrivacy;
string code2 = "1";
string code = words[2].ToString().ToLower();
if (code.ToLower() == "error")
{
code2 = "1";
}
if (code.ToLower() == "warning")
{
code2 = "2";
}
if (code.ToLower() == "information")
{
code2 = "3";
}
if (code.ToLower() == "success")
{
code2 = "4";
}
if (code.ToLower() == "failure")
{
code2 = "5";
}
string queryStr = "select * from Win32_NTLogEvent where Logfile = '";
queryStr += words[1].ToString();
queryStr += "' and EventType = " + code2 + "";
System.Net.IPHostEntry h =
System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
string IPAddress = h.AddressList.GetValue(0).ToString();
string lm = System.Net.Dns.GetHostName().ToString();
if (txtUsername.Text == "" || txtPassword.Text == ""
|| txtServer.Text == "127.0.0.1"
|| txtServer.Text.ToLower() == "localhost"
|| txtServer.Text.ToLower() == lm.ToLower()
|| txtServer.Text.ToLower() == IPAddress.ToLower())
{
co.Username = null;
co.Password = null;
}
string temp1 = txtServer.Text;
System.Management.ManagementScope ms =
new System.Management.ManagementScope("\\\\" + temp1 + "\\root\\cimv2", co);
ObjectQuery oQuery = new ObjectQuery(queryStr);
//Execute the query
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(ms, oQuery);
//Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();
this.Label1.Style["text-align"] = "left";
Label1.ForeColor = Color.Black;
Label1.Text = "<br />Viewing Most Recent <b>" +
words[1].ToString().ToLower() + "</b> log entries: " +
words[2].ToString().ToLower() + "<br />";
Label1.Text += "-----------------------------------" + "<br>";
foreach (ManagementObject oReturn in oReturnCollection)
{
Label1.Text += "<b>Time Written:</b> " + oReturn["TimeWritten"] + "<br>";
Label1.Text += "<b>Event Code:</b> " + oReturn["EventCode"] + "<br>";
Label1.Text += "<b>Message:</b> " + oReturn["Message"] + "<br>";
Label1.Text += "-----------------------------------" + "<br>";
//oReturn.InvokeMethod("ClearEventLog", obj);
//oReturn.Dispose();
}
Various Scripts and Application Code Segments for .NET, VB, C#, C++, C, Java, JavaScript, HTML, Python, Perl, AutoIT, Batch, ASP Classic, Objective-C, Swift, Unreal Engine 4, Unity3D & others. Also contains numerous IT tidbits, procedures, and tricks including Technology Hacks on various platforms.
Subscribe to:
Post Comments (Atom)
Generating "Always On Top" NSWindow in macOS across all detected displays
Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift Langues, you m...
-
Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift Langues, you m...
-
This simple script is how to submit scores and points to the Game Center from a Unity3D Game. People use the prime31 plugin, and pay the e...
-
Recently Possess () has been deprecated from UE4 , and when writing classes based on AAIController you have to use the function OnPossess ...

No comments:
Post a Comment