Click here to Skip to main content
15,880,469 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 254.7K   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

 
QuestionWorking locally only Pin
a7madhazim30-Jul-18 21:48
a7madhazim30-Jul-18 21:48 
QuestionCannot connect to ip or Computer name Pin
Ma'moun Omer1-Jan-17 21:39
Ma'moun Omer1-Jan-17 21:39 
QuestionExcellent Pin
Ed Gadziemski5-Jan-14 6:20
professionalEd Gadziemski5-Jan-14 6:20 
QuestionThe Demo Code does not show how to use the GetNetworkAddresses method in the DLL Pin
Member 107836015-Mar-11 7:09
Member 107836015-Mar-11 7:09 
QuestionAccess denied : odd problem? Pin
ms.linuz30-Aug-10 17:37
ms.linuz30-Aug-10 17:37 
QuestionScanning systems with network turned off or wireless turned off Pin
Charles Clampitt17-Sep-09 6:05
Charles Clampitt17-Sep-09 6:05 
Generalneed help Pin
gvrkrish11-Aug-09 21:53
gvrkrish11-Aug-09 21:53 
GeneralMy vote of 1 Pin
Dave Kreskowiak10-Feb-09 10:10
mveDave Kreskowiak10-Feb-09 10:10 
QuestionError is coming Pin
vivekanandaraj15-Jul-08 11:06
vivekanandaraj15-Jul-08 11:06 
Questionhow to change the time of a remote system? Pin
kk_upadhyay29-Nov-07 22:55
kk_upadhyay29-Nov-07 22:55 
QuestionRe: how to change the time of a remote system? Pin
shahidriazonline13-Nov-08 0:53
shahidriazonline13-Nov-08 0:53 
GeneralGetNetworkadresses -> missing i++ Pin
iceteatwo25-Nov-07 1:40
iceteatwo25-Nov-07 1:40 
QuestionUnable to connect to RPC service: The RPC server is unavailable.The program '[5228] ShowInformation.exe' has exited with code 0 (0x0). Pin
Amit Papriwal19-Oct-07 2:19
Amit Papriwal19-Oct-07 2:19 
AnswerRe: Unable to connect to RPC service: The RPC server is unavailable.The program '[5228] ShowInformation.exe' has exited with code 0 (0x0). Pin
Crazyloon (N. Smith)22-Oct-07 4:28
Crazyloon (N. Smith)22-Oct-07 4:28 
Make sure you have Administrator rights to logon to the machine and also that no firewall is blocking.

"Dream as if you'll live forever. Live as if you'll die tomorrow."

GeneralRe: Unable to connect to RPC service: The RPC server is unavailable.The program '[5228] ShowInformation.exe' has exited with code 0 (0x0). Pin
Amit Papriwal7-Nov-07 2:52
Amit Papriwal7-Nov-07 2:52 
QuestionRe: Unable to connect to RPC service: The RPC server is unavailable.The program '[5228] ShowInformation.exe' has exited with code 0 (0x0). Pin
zawmn8328-Mar-08 0:10
zawmn8328-Mar-08 0:10 
AnswerRe: Unable to connect to RPC service: The RPC server is unavailable.The program '[5228] ShowInformation.exe' has exited with code 0 (0x0). [modified] Pin
Jay Ramavat9-Jul-09 23:25
Jay Ramavat9-Jul-09 23:25 
GeneralE_AccessDenied Pin
stiger1218-Oct-07 7:51
stiger1218-Oct-07 7:51 
AnswerRe: E_AccessDenied Pin
Cool Cassis8-Nov-07 8:16
Cool Cassis8-Nov-07 8:16 
QuestionAccessing remote computer's WMI Pin
wicked weapon26-Sep-07 17:44
wicked weapon26-Sep-07 17:44 
QuestionWHERE CAN I FIND THE V1.2 FULL BINARY? Pin
magaupe18-Sep-07 7:02
magaupe18-Sep-07 7:02 
GeneralThanks! Good job! Pin
tommy_il24-Jun-07 4:46
tommy_il24-Jun-07 4:46 
GeneralGet the Current time of a remote client machine in c# Pin
prgramya14-Jun-07 18:36
prgramya14-Jun-07 18:36 
QuestionNeed help in getting remote system info Pin
ghkishore25-Mar-07 21:19
ghkishore25-Mar-07 21:19 
AnswerRe: Need help in getting remote system info Pin
Crazyloon (N. Smith)26-Mar-07 3:46
Crazyloon (N. Smith)26-Mar-07 3:46 

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.