Click here to Skip to main content
15,889,216 members
Articles / Web Development / IIS
Article

Collecting Remote System Information With WMI

Rate me:
Please Sign up or sign in to vote.
3.45/5 (16 votes)
15 Nov 20044 min read 255K   11.4K   83   47
An article explaining how to connect and collect information from a remote computer using the WMI interface.

Image 1

Introduction

Recently while doing the inventory of several servers I realized that it was cumbersome to have to logon to each one and access the default Windows System Information utility. After doing some reading I found the WMI classes in the MSDN. While these classes allow for access to just about anything on a Windows operating system, they require a lot of typing and calls to multiple WMI classes. There is not just one place to access everything. This class solves that.

Background

This class is a wrapper for some basic WMI interfaces that provide detailed information about a system. The class allows the use of the current security context or a specified username and password to access either a remote or a local machine. Once connected the class is then populated with the values from the machine. This gives us an easy way of accessing the information without having to worry about the calls to each of the WMI interfaces or how to store all the information. I'm not going to spend a lot of time explaining the WMI or what it is other than that it is an interface for accessing the management information on a computer. If you would like to have more reading material on the subject then check out the following link Windows Management Instrumentation at Microsoft.

Using the code

This wrapper class is designed to allow for easy access of information related to a system whether local or remote. Once the class is initialized with the data for a system there is no further need to connect to the machine. If done separately a user would need to make multiple calls to each WMI interface and then store the information. The class does all of this.

To begin using the code start by adding a reference to the DLL. To populate the class with the information from the local machine pass in the loopback address as shown below:

C#
//
// Create an instance of the class.
//
SystemInformation sysinfo = new SystemInformation();
//
// Set the output error stream to the standard console output.
//
sysinfo.stderr = Console.OpenStandardOutput();
//
// Populate the class and make sure it didn't fail.
//
if (sysinfo.Get("127.0.0.1") != 0)
{
  MessageBox.Show("Error getting system information.", "System Information");
}
else
{
  MessageBox.Show("The current OS version is " + sysinfo.OSVersion.description, 
                                                        "System Information");
}
...

In the above example the stderr property is also set. This property allows the internal errors that occur in the wrapper class to be piped to an external stream. In this example, the standard output stream for the Console class is used. This little trick allows for internal debugging information to be controlled by the calling application so that the SystemInformationclass doesn't have to worry about logging.

To access the information from a remote machine you can simply change the address given above to that of an external machine. When connecting to a remote or local machine the current user context is used to authenticate the user. This presents a problem if the current context does not have access to the machine. An example of this would be if this class was used in a web application which was running as the anonymous user account. To solve this problem you may pass user credentials to the Get method of the class as shown below:

C#
//
// Create an instance of the class.
//
SystemInformation sysinfo = new SystemInformation();
//
// Set the output error stream to the standard console output.
//
sysinfo.stderr = Console.OpenStandardOutput();
//
// Populate the class and make sure it didn't fail.
//
if (sysinfo.Get("127.0.0.1", "johndoe", "password") != 0)
{
  MessageBox.Show("Error getting system information.", "System Information");
}
else
{
  MessageBox.Show("The current OS version is " + sysinfo.OSVersion.description, 
                                                        "System Information");
}
...

When this is done the user specified is impersonated when connecting to the remote machine. The username can also contain a domain if necessary. Note that if the username or password are not valid on the machine you're connecting to the method will fail and return an error code.

The included demo project connects to the local machine and displays all the information in a similar fashion as seen by the System Information utility included with Windows. It is not a fully complete application but only meant to demonstrate the capabilities of the class.

Points of interest

The above code has only been tested against Windows 2000/2003/XP. It may work against other systems but I have not tested it. Also in order for the class to connect to a computer the RPC service must be available. Most firewalls block this sort of traffic.

In the class you may notice that there are two methods used to query the interfaces. The first method uses the ManagementObjectSearcher class. The other uses the ManagementClass class. While both of these perform similar tasks and return a collection of data the latter does not allow the specification of which attributes to return or the use of a filter by means of the WHERE clause.

One problem I saw when querying the data was that the time zone was returned as an integer representing the offset from GMT time in minutes. While this is nice if you need to do computations, it is difficult to read. While it would have been possible to build a static list of all the time zones out there and map the value to this I chose to do it in a more dynamic way. Windows stores all the information on the time zones in the registry. By querying these keys you can determine what time zone the minutes map to. The structure populates with the time zone information, therefore contains both the offset in minutes and the readable version of the time zone.

History

  • v1.0
    • Initial build. Nothing special.
  • v1.1
    • Added the ability to get the standard time zone name from the registry to make it more readable.
  • v1.2
    • The class now returns proper error codes and has a new method to allow access to the extended error code if applicable. Also modified the sample to allow connecting to multiple hosts.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have been developing software professionally for more than 5 years, mostly web based with ASP and .Net. I have had a passion for programming though my whole life.

My current web site at http://www.simplyneatsoftware.com/ offers more projects under development and articles of interest.

If i'm not programming I am usually play golf or billards or just relaxing at a football game with a cigar and beer in hand.

Comments and Discussions

 
Generalgetlogicaldrves info of a remote machine Pin
deepualuru29-Nov-06 17:31
deepualuru29-Nov-06 17:31 
GeneralRe: getlogicaldrves info of a remote machine Pin
Crazyloon (N. Smith)19-Dec-06 9:24
Crazyloon (N. Smith)19-Dec-06 9:24 
NoneError when adapter is disabled [modified] Pin
codecaine14-Jul-06 6:04
codecaine14-Jul-06 6:04 
GeneralError on remote connections [modified] Pin
Marcos Accioly22-May-06 2:48
Marcos Accioly22-May-06 2:48 
QuestionExceptions Pin
jk8526-Apr-06 23:00
jk8526-Apr-06 23:00 
GeneralAccess Denied Fixes Pin
El Chubb28-Mar-06 15:16
El Chubb28-Mar-06 15:16 
GeneralRe: Access Denied Fixes Pin
stiger1218-Oct-07 6:31
stiger1218-Oct-07 6:31 
GeneralThanks Pin
raj012345624-Dec-05 12:06
raj012345624-Dec-05 12:06 
GeneralUrgent!!!!!!!!!!!!!! Pin
neethu130-Aug-05 0:34
neethu130-Aug-05 0:34 
GeneralRe: Urgent!!!!!!!!!!!!!! Pin
Vadim Tabakman20-Apr-06 19:42
Vadim Tabakman20-Apr-06 19:42 
GeneralThis does nor wor on some machines Pin
idht2-Aug-05 7:37
idht2-Aug-05 7:37 
GeneralRe: This does nor wor on some machines Pin
Crazyloon (N. Smith)16-Aug-05 11:41
Crazyloon (N. Smith)16-Aug-05 11:41 
GeneralRe: This does nor wor on some machines Pin
zubairyounas23-Nov-05 1:40
zubairyounas23-Nov-05 1:40 
AnswerRe: This does nor wor on some machines Pin
idht23-Nov-05 7:50
idht23-Nov-05 7:50 
GeneralRe: This does nor wor on some machines Pin
zubairyounas24-Nov-05 18:56
zubairyounas24-Nov-05 18:56 
GeneralRe: This does nor wor on some machines Pin
Peppe DS4-May-06 4:24
Peppe DS4-May-06 4:24 
GeneralRe: This does nor wor on some machines Pin
Timur_K1-Aug-06 12:27
Timur_K1-Aug-06 12:27 
GeneralTim Zone Pin
45South24-Nov-04 9:25
45South24-Nov-04 9:25 
GeneralRe: Tim Zone Pin
Crazyloon (N. Smith)28-Nov-04 15:44
Crazyloon (N. Smith)28-Nov-04 15:44 
GeneralRe: Tim Zone Pin
45South28-Nov-04 15:46
45South28-Nov-04 15:46 
GeneralSP2 and WMI Pin
Mario M.17-Nov-04 10:36
Mario M.17-Nov-04 10:36 
GeneralRe: SP2 and WMI Pin
Crazyloon (N. Smith)17-Nov-04 10:56
Crazyloon (N. Smith)17-Nov-04 10:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.