WMI is very helpful to access remote machine details.
Introduction
Windows Management Instrumentation(WMI) Provides access to a rich set of management information and management events about the system, devices, and applications instrumented to the Windows Management Instrumentation (WMI) infrastructure.
Objective
To understand about basics of WMI.
Using the code
We can read the logs with the help of Event Log class but
when I tried to access remote server logs then it failed and started throwing an
error. Using Event Log class we can access only local machine logs.
In order to access the remote server details we should use
WMI.
WMI gives a way to .net developers to connect to remote
machines and access of it.
With the help of WMI we can access remote machine details like
event logs, disk space, services, printers, USB details etc.
Let’s try to understand WMI with simple example.
WMI classes reside under system.Management namespace.
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Xml.Linq;
using System.Management;
using
System.Diagnostics;
using
System.Collections;
namespace WMI
{
class EventLogDetails
{
public string Message { get;
set; }
public string Type { get; set; }
public DateTime TimeGenerated { get;
set; }
public string Category {get;set;}
public string MachineName {get;set;}
public string SourceName{get;set;}
public DateTime TimeWritten{get;set;}
public string User{get;set;}
/// <summary>
///
/// </summary>
/// <returns></returns>
public Dictionary<string,
string> GetInfo()
{
//dictionary
object to hold the values
Dictionary<string, string>
dictInfo = new Dictionary<string, string>();
dictInfo.Add("Message", "");
dictInfo.Add("Source", "");
dictInfo.Add("Type
", "");
dictInfo.Add("Category
", "");
dictInfo.Add("TimeWritten",
"");
dictInfo.Add("MachineName",
"");
//create
our WMI searcher
ManagementPath
mPath_EventLog;
ManagementScope
mgmtScope;
ManagementObjectCollection
moc;
ManagementClass
mc;
ConnectionOptions
options;
System.Management.ObjectQuery qury = new
ObjectQuery();
string
Host = "Machine Name";
// Remote machine
connection details
options = new
ConnectionOptions();
options.Username = "UserName";
options.Password = "Password";
options.Authority = "ntlmdomain:" + " XYZ ";
options.Authentication = AuthenticationLevel.PacketPrivacy;
options.EnablePrivileges = true;
//By default impersonation is Impersonate
options.Impersonation = ImpersonationLevel.Impersonate;
mgmtScope = new ManagementScope("\\\\" + Host + "\\root\\cimv2",
options);
mPath_EventLog = new ManagementPath();
//execute
the query using WMI
qury = new
ObjectQuery("Select
* from Win32_NTLogEvent
Where (Logfile = 'Application') AND (Type =
'Error')");
ManagementObjectSearcher
searcher = new ManagementObjectSearcher(mgmtScope,
qury);
//Create a XML doc
var
xdoc = new XDocument();
XElement
xroot = new XElement("root");
xdoc.Add(xroot);
//now
loop through all the item found with the query
foreach
(ManagementObject obj in searcher.Get())
{
dictInfo = new Dictionary<string, string>();
dictInfo ["Message"] = obj.Properties["Message"].Value.ToString();
dictInfo ["Source"] = obj.Properties["Source"].Value.ToString();
dictInfo ["Type"] = obj.Properties["Type"].Value.ToString(); dictInfo ["Category"] = obj.Properties["Category"].Value.ToString();
dictInfo ["TimeWritten"] = obj.Properties["TimeWritten"].Value.ToString();
dictInfo ["MachineName"] = obj.Properties["MachineName"].Value.ToString();
xroot.Add(dictInfo.Select(ent
=> new XElement(ent.Key,
ent.Value)));
//Save the xml
xdoc.Save("D:\\Test\\WMI.xml", SaveOptions.None);
}
//return
the info
return
dictInfo;
}
}
}
Now all the details will be saved in WMI.xml file. Next step
is to retrieve the required information from xml and display it.
Conclusion
Have a fun with exploring WMI classesJ.
Please provide feedback below.
Reference
MSDN