Click here to Skip to main content
Click here to Skip to main content

Collecting Remote System Information With WMI

, 15 Nov 2004
Rate this:
Please Sign up or sign in to vote.
An article explaining how to connect and collect information from a remote computer using the WMI interface.

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:

//
// 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:

//
// 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

Share

About the Author

Crazyloon (N. Smith)
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

 
QuestionExcellent PinprofessionalEd Gadziemski5-Jan-14 6:20 
QuestionThe Demo Code does not show how to use the GetNetworkAddresses method in the DLL PinmemberMember 107836015-Mar-11 7:09 
QuestionAccess denied : odd problem? Pinmemberms.linuz30-Aug-10 17:37 
QuestionScanning systems with network turned off or wireless turned off PinmemberCharles Clampitt17-Sep-09 6:05 
Generalneed help Pinmembergvrkrish11-Aug-09 21:53 
GeneralMy vote of 1 PinmvpDave Kreskowiak10-Feb-09 10:10 
QuestionError is coming Pinmembervivekanandaraj15-Jul-08 11:06 
Questionhow to change the time of a remote system? Pinmemberkk_upadhyay29-Nov-07 22:55 
QuestionRe: how to change the time of a remote system? Pinmembershahidriazonline13-Nov-08 0:53 
GeneralGetNetworkadresses -> missing i++ Pinmembericeteatwo25-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). PinmemberAmit 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). PinmemberCrazyloon (N. Smith)22-Oct-07 4:28 
GeneralRe: Unable to connect to RPC service: The RPC server is unavailable.The program '[5228] ShowInformation.exe' has exited with code 0 (0x0). PinmemberAmit 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). Pinmemberzawmn8328-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] PinmemberJay Ramavat9-Jul-09 23:25 
GeneralE_AccessDenied Pinmemberstiger1218-Oct-07 7:51 
AnswerRe: E_AccessDenied PinmemberCool Cassis8-Nov-07 8:16 
QuestionAccessing remote computer's WMI Pinmemberwicked weapon26-Sep-07 17:44 
QuestionWHERE CAN I FIND THE V1.2 FULL BINARY? Pinmembermagaupe18-Sep-07 7:02 
GeneralThanks! Good job! Pinmembertommy_il24-Jun-07 4:46 
GeneralGet the Current time of a remote client machine in c# Pinmemberprgramya14-Jun-07 18:36 
QuestionNeed help in getting remote system info Pinmemberghkishore25-Mar-07 21:19 
AnswerRe: Need help in getting remote system info PinmemberCrazyloon (N. Smith)26-Mar-07 3:46 
Generalgetlogicaldrves info of a remote machine Pinmemberdeepualuru29-Nov-06 17:31 
GeneralRe: getlogicaldrves info of a remote machine PinmemberCrazyloon (N. Smith)19-Dec-06 9:24 
NoneError when adapter is disabled [modified] Pinmembercodecaine14-Jul-06 6:04 
GeneralError on remote connections [modified] PinmemberMarcos Accioly22-May-06 2:48 
QuestionExceptions Pinmemberjk8526-Apr-06 23:00 
GeneralAccess Denied Fixes PinmemberEl Chubb28-Mar-06 15:16 
GeneralRe: Access Denied Fixes Pinmemberstiger1218-Oct-07 6:31 
GeneralThanks Pinmemberr_konjeti12324-Dec-05 12:06 
GeneralUrgent!!!!!!!!!!!!!! Pinmemberneethu130-Aug-05 0:34 
GeneralRe: Urgent!!!!!!!!!!!!!! PinmemberJubjub20-Apr-06 19:42 
GeneralThis does nor wor on some machines Pinmemberidht2-Aug-05 7:37 
GeneralRe: This does nor wor on some machines PinmemberCrazyloon (N. Smith)16-Aug-05 11:41 
GeneralRe: This does nor wor on some machines Pinmemberzubairyounas23-Nov-05 1:40 
AnswerRe: This does nor wor on some machines Pinmemberidht23-Nov-05 7:50 
GeneralRe: This does nor wor on some machines Pinmemberzubairyounas24-Nov-05 18:56 
GeneralRe: This does nor wor on some machines PinmemberPeppe DS4-May-06 4:24 
GeneralRe: This does nor wor on some machines Pinmemberteeekay1-Aug-06 12:27 
GeneralTim Zone Pinmember45South24-Nov-04 9:25 
GeneralRe: Tim Zone PinmemberCrazyloon (N. Smith)28-Nov-04 15:44 
GeneralRe: Tim Zone Pinmember45South28-Nov-04 15:46 
GeneralSP2 and WMI PinmemberMario M.17-Nov-04 10:36 
GeneralRe: SP2 and WMI PinmemberCrazyloon (N. Smith)17-Nov-04 10:56 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141030.1 | Last Updated 15 Nov 2004
Article Copyright 2004 by Crazyloon (N. Smith)
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid