Introducing Windows Management Instrumentation (WMI)
page 4 of 4
by Steven Swafford
Feedback
Average Rating: 
Views (Total / Last 10 Days): 27674/ 52

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.


View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-08 9:07:19 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search