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

Collecting Remote System Information With WMI

By , 15 Nov 2004
 

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

About the Author

Crazyloon (N. Smith)
Web Developer
United States United States
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionThe Demo Code does not show how to use the GetNetworkAddresses method in the DLLmemberMember 107836015 Mar '11 - 7:09 
QuestionAccess denied : odd problem?memberms.linuz30 Aug '10 - 17:37 
QuestionScanning systems with network turned off or wireless turned offmemberCharles Clampitt17 Sep '09 - 6:05 
Generalneed helpmembergvrkrish11 Aug '09 - 21:53 
GeneralMy vote of 1mvpDave Kreskowiak10 Feb '09 - 10:10 
QuestionError is comingmembervivekanandaraj15 Jul '08 - 11:06 
Questionhow to change the time of a remote system?memberkk_upadhyay29 Nov '07 - 22:55 
QuestionRe: how to change the time of a remote system?membershahidriazonline13 Nov '08 - 0:53 
GeneralGetNetworkadresses -> missing i++membericeteatwo25 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).memberAmit 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).memberCrazyloon (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).memberAmit 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).memberzawmn8328 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]memberJay Ramavat9 Jul '09 - 23:25 
GeneralE_AccessDeniedmemberstiger1218 Oct '07 - 7:51 
AnswerRe: E_AccessDeniedmemberCool Cassis8 Nov '07 - 8:16 
QuestionAccessing remote computer's WMImemberwicked weapon26 Sep '07 - 17:44 
QuestionWHERE CAN I FIND THE V1.2 FULL BINARY?membermagaupe18 Sep '07 - 7:02 
GeneralThanks! Good job!membertommy_il24 Jun '07 - 4:46 
GeneralGet the Current time of a remote client machine in c#memberprgramya14 Jun '07 - 18:36 
QuestionNeed help in getting remote system infomemberghkishore25 Mar '07 - 21:19 
AnswerRe: Need help in getting remote system infomemberCrazyloon (N. Smith)26 Mar '07 - 3:46 
Generalgetlogicaldrves info of a remote machinememberdeepualuru29 Nov '06 - 17:31 
GeneralRe: getlogicaldrves info of a remote machinememberCrazyloon (N. Smith)19 Dec '06 - 9:24 
NoneError when adapter is disabled [modified]membercodecaine14 Jul '06 - 6:04 

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

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