WMI Code Creator - Great tool for WMI Programming tasks

by Viper 4. May 2009 12:37

Every time somebody says Use WMI to do this or that, it gives me heebie-jeebies. I start talking to my self, now I will have to figure out what WMI class to use for this task, what properties and methods are exposed and how to write SQL like statement to accomplish what I am trying to do. Last week I was trying to create an automated tool to test load balancing on web servers. Part of the task is to switch to turn off network adapters at certain intervals and turn them on. All help on this task pointed to making use of WMI. There you go, something I really did not want to hear but I had to do.

During this process of creating my nifty tool for automated testing, I found out about WMI Code Creator. Even though it was publish 4 years ago, it is a very useful tool and must have if you are doing WMI programming. Not only it lists all the classes exposed for various tasks, it tells you the properties, methods etc. for each of those classes. And best part is that it generates some boiler plate code as well in C#, VB.Net and VB script. What more could one ask for. With click of few menu options, I was able to generate following code that lists all network adapters on server and spits out the properties of each device.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;

namespace WebMonitor
{
class Program
{
static void Main(string[] args)
{
try
{
ManagementObjectSearcher searcher =
 new ManagementObjectSearcher("root\\CIMV2",
 "SELECT * FROM Win32_NetworkAdapter");
 foreach (ManagementObject queryObj in searcher.Get())
 {
	Console.WriteLine
		("----------------------------");
	Console.WriteLine
		("AdapterType: {0}", queryObj["AdapterType"]);
	Console.WriteLine
		("AdapterTypeId: {0}", queryObj["AdapterTypeId"]);
	Console.WriteLine
		("AutoSense: {0}", queryObj["AutoSense"]);
	Console.WriteLine
		("Availability: {0}", queryObj["Availability"]);
	Console.WriteLine
		("Caption: {0}", queryObj["Caption"]);
	Console.WriteLine
		("ConfigManagerErrorCode: {0}", queryObj["ConfigManagerErrorCode"]);
	Console.WriteLine
		("ConfigManagerUserConfig: {0}", queryObj["ConfigManagerUserConfig"]);
	Console.WriteLine
		("CreationClassName: {0}", queryObj["CreationClassName"]);
	Console.WriteLine
		("Description: {0}", queryObj["Description"]);
	Console.WriteLine
		("DeviceID: {0}", queryObj["DeviceID"]);
	Console.WriteLine
		("ErrorCleared: {0}", queryObj["ErrorCleared"]);
	Console.WriteLine
		("ErrorDescription: {0}", queryObj["ErrorDescription"]);
	Console.WriteLine
		("Index: {0}", queryObj["Index"]);
	Console.WriteLine
		("InstallDate: {0}", queryObj["InstallDate"]);
	Console.WriteLine
		("Installed: {0}", queryObj["Installed"]);
	Console.WriteLine
		("LastErrorCode: {0}", queryObj["LastErrorCode"]);
	Console.WriteLine
		("MACAddress: {0}", queryObj["MACAddress"]);
	Console.WriteLine
		("Manufacturer: {0}", queryObj["Manufacturer"]);
	Console.WriteLine
		("MaxNumberControlled: {0}", queryObj["MaxNumberControlled"]);
	Console.WriteLine
		("MaxSpeed: {0}", queryObj["MaxSpeed"]);
	Console.WriteLine
		("Name: {0}", queryObj["Name"]);
	Console.WriteLine
		("NetConnectionID: {0}", queryObj["NetConnectionID"]);
	Console.WriteLine
		("NetConnectionStatus: {0}", queryObj["NetConnectionStatus"]);
 }
 catch (ManagementException e)
 {
	Console.WriteLine("An error occurred while querying for WMI data: " + e.Message);
 }
 }
 }
}

Give your advice to big bosses and make money

Views: 808

Tags:

.Net | WMI

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli

By Categories