AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=629&pId=-1
Introducing Windows Management Instrumentation (WMI)
page
by Steven Swafford
Feedback
Average Rating: 
Views (Total / Last 10 Days): 27721/ 53

Windows Management Instrumentation Overview

[ download code ]

Windows Management Instrumentation (WMI) is part of the Windows operating system that provides management information and control. From a developer’s viewpoint WMI can provide event monitoring for your application or simply report as this article will demonstrate. As well WMI may be used to access and establish information relating to desktop/laptop systems, applications, and networks just to name a few. If you wish to read greater details concerning WMI please visit Platform SDK: Windows Management Instrumentation.

System Requirements

WMI is currently included with Windows 2000, Windows XP, or Windows ME edition. While with Windows 95, Windows 98, and Windows NT 4.0 you must download a separate WMI installation package from MSDN.

Downloads

Now that I have provided an overview of WMI, system requirements, and a variety of downloads to assist you learning more, it is now to move into an example web based solution that will allow an end user to visit a web page that collects various details such as the MAC Address, CPU, hard drive, and operating system information.

Interfacing with WMI instances

[ download code ]

I will discuss two radically different methods of interfacing with WMI instances both of which are fairly simple and straight forward. Basically it comes down to a personal choice and the requirements defined by the application that you are writing.

WMI Query Language (WQL) allows you to query WMI providers using a SQL-like query language, so if you are familiar with SQL then you will find WQL all to familiar. If you know the provider classes and the fields available, then you can get the info very easily.  For instance, if you wanted to get a list of logical drives from a system you would use the following query:

Select * from Win32_LogicalDisk

You can, however, refine the search by using where clauses and getting specific "fields" in the query. The following query gets the amount of free space, the size, and the name of all fixed disk drives:

Select FreeSpace, Size, Name from Win32_LogicalDisk where DriveType = 3

If you are not sure what provider classes and fields are available to you then this is where the WMI Administrative Tools will come in handy. Take the following steps:

  • Download and install the WMI Administrative Tools.
  • Once installed under the WMI Tools program group, click "WMI Object Browser".

The object browser will allow you to view the provider classes and associated field within a given class. Upon running the object browser you will be presented a screen as follows:

Figure 1
Figure 1

  • Click "OK".

Figure 2
Figure 2

  • Click OK. Then you will be presented a screen similar to the following:

Figure 3
Figure 3

As you can see in the above instance we are looking at the Win32_ComputerSystem provider class on the left hand side and the available fields on the right hand side of the above screen. In order to perform WQL queries in you class you will need to import the System.Management Namespace. The classes we will discuss are:

Build your first WQL Query

[ download code ]

Let us say that we want to gather build number, build type, and version of the operating system. In order to do this we first need to establish the WQL query. In this case we will utilize the following query:

OS WQL Query

SELECT * FROM Win32_OperatingSystem
Figure 4

As you are probably all too familiar with you really should not SELECT * in any query whether SQL or WQL unless you are going to utilize all the return results. So our OS WQL query really should look like the following:

SELECT BuildNumber, BuildType, Version FROM Win32_OperatingSystem
Figure 4-1

Example GetOperatingSystemDetails Method

public ArrayList GetOperatingSystemDetails()
{
 ArrayList osCollection = new ArrayList();
 ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT BuildNumber, BuildType, Version FROM Win32_OperatingSystem");
 
 foreach(ManagementObject osObject in mos.Get())
 {
  IOperatingSystem os = new IOperatingSystem();
  os.BuildNumber = osObject["BuildNumber"].ToString();
  os.BuildType = osObject["BuildType"].ToString();
  os.Version = osObject["Version"].ToString();
  osCollection.Add(os);
 }
 return osCollection;
}
Figure 5

The method GetOperatingSystemDetails performs four separate actions.

  • Establishes a new ArrayList named osCollection.
  • Establishes a new ManagementObjectSearcher named mos.
  • Loops thru the osObject and adds each object to osCollection.
  • Finally the osCollection is returned.

Now that you have been provided the foundation of writing a WQL query to gather operating system details let us now apply this instruction into a working demo using a DataGrid. We will utilize both the example query and method above (see figure 4 and 5).

Within your IDE create a DataGrid and name it OperatingSystemDataGrid.

<asp:DataGrid id="OperatingSystemDataGrid" runat="server"></asp:DataGrid>
Figure 6

If you recall the GetOperatingSystemDetails method all you need to do at this point is establish a new method which will in turn set the OperatingSystemDatagrid’s DataSource as well as DataBind the DataGrid.

Example

public void BindOperatingSystemDataGrid()
{
 OperatingSystemDataGrid.DataSource = GetOperatingSystemDetails();
 OperatingSystemDataGrid.DataBind();
}
Figure 7

Now in you page load method simply call the BindOperatingSystemDataGrid() method.

private void Page_Load(object sender, System.EventArgs e)
{
 // Put user code to initialize the page here
 BindOperatingSystemDataGrid();
}
Figure 8

Now execute this web page in your browser and you should have a DataGrid similar to the following.

Figure 9
Figure 9

As you can see it really is not all that difficult to use WQL and construct the code that will in turn display the desire information to the end user. Now that you have the foundation of WQL there is another avenue we can take to achieve the same results of which I will discuss in greater details.

Using the ManagementClass to Gather Operating System Details

[ download code ]

Previously, we use the ManagementObject and ManagaementObjectSearcher classes where in this part of the article we will take a different approach of gathering the operating system details. Once again we want to pull the BuildNumber, BuildType, and Version of the operating System. To accomplish this we will look at the following classes:

Example GetOperatingSystemInfo() Method

public string GetOperatingSystemInfo()
{
 ManagementClass managementClass = new ManagementClass("Win32_OperatingSystem");
 ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
 StringBuilder sbOperatingSystem = new StringBuilder();


 foreach(ManagementObject managementObject in managementObjectCollection)
 {
  if(operatingSystemInfo == null)
  {
   sbOperatingSystem.Append("Version: " + managementObject["Version"].ToString() +
    "<br>Build Number: " + managementObject["BuildNumber"].ToString() +
    "<br>Build Type: " + managementObject["BuildType"].ToString());
  }
  managementObject.Dispose();
 }


 OperatingSystemLabel.Text = sbOperatingSystem.ToString();
 return operatingSystemInfo = sbOperatingSystem.ToString();
}
Figure 10

As you can see in the example the first thing I do in to instantiate a new ManagementClass passing in the Win32_OperatingSystem provider class available from WMI. Next you need to instantiate a ManagementObjectColletion that will contain the management objects received from your managementClass. Finally the last thing you need to do is establish a new StringBuilder object that we will use to return the operating information in the form of a string and assign it to a Label that is used in our Web Form.

Within your IDE created a Label and name it OperatingSystemLabel.

<asp:Label id="OperatingSystemLabel" runat="server"></asp:Label>
Figure 11

If you recall the GetOperatingSystemInfo method all you need to do at this point is call this method. For purposes of this example we will use the Page Load event.

Example Page Load event

private void Page_Load(object sender, System.EventArgs e)
{
 // Put user code to initialize the page here
 GetOperatingSystemInfo();
}
Figure 12

Now it is time to execute the Web Form in your browser and see first hand the fruits of your labor. If everything had executed properly you should see a screen similar to the following:

Figure 13
Figure 13

This article has introduced you to WQL within WMI as well I have provided two different examples of retrieving operating system information. There are a few uses that I can think of at this moment. One would be providing the capability to email the results to a system administrator and second and probably even better would be to store the information within a database for future reference. I hope this introduction has perked your curiosity and in the future part II of this article will cover those fields that you can write to.

Bonus: Here are a couple of methods to wet your whistle.

How do I determine what domain a machine is part of as well as the workstation name?

public string GetDomain()
{
 ArrayList csCollection = new ArrayList();
 ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
 StringBuilder sb = new StringBuilder();


 foreach(ManagementObject osObject in mos.Get())
 {
  sb.Append("Domain: " + osObject["Domain"].ToString() +
   "\n\n Computer Name: " + osObject["Name"].ToString());
 }
 return sb.ToString();
}

How do I obtain the system running on the windows computer?

public string GetSystemType()
{
 ArrayList csCollection = new ArrayList();
 ManagementObjectSearcher mos = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
 StringBuilder sb = new StringBuilder();


 foreach(ManagementObject osObject in mos.Get())
 {
  sb.Append("System Type: " + osObject["SystemType"].ToString());
 }
 return sb.ToString();
}

Suggested Readings

Feel free to discuss this article at my Blog.



©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-24 7:25:06 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search