How System Resource Information is Retrieved From WMI

Introduction

Organizations have 600 servers running Windows Serer 2003. Seventy percent of these servers are used in production and the rest in development. The systems administrators in your organization need to know exactly what services are running on which servers. The systems administrators often need to make emergency changes to deal with production issues. You need to generate a weekly report that details the services running on each server, such as the service account under which the services are running, and whether the services are disabled or set to start automatically or manually. You can generate this report by retrieving system resource information from WMI. You can retrieve information about system resources using the following query strings.

Query strings help to retrieve information about system resources, such as , services that are running on a server from WMI. The query strings are:

  1. Select * From Win32_Service: this WQL query returns all properties from each Windows Service running on the computer. The win32_serrvice class represents a service on the Windows operating system.
  2. Select * from win32_networkconnection: This WQL statement returns all properties of all network connections made from a computer .
  3. SELECT * FROM Win32_DriverVXD: This WQL Statement returns information about all virtual device drivers on the Windows operating system. A virtual device is a program that manages system resources, such as a hardware device or installed software, so that multiple applications can use the resource simultaneously.
  4. SELECT 8 FROM Win32_Process: This WQL statement returns all processes that are currently executing in the Windows operating system.
  5. SELECT * FROM Win32_NetworkAdapter: This WQL statement returns properties for all network adapters installed on the server. The Win32_NetworkAdapter class depicts a network adapter on a computer with the Windows operating system. This class is derived from CIM_NetworkAdapter.

The following code sample shows how to use query strings to query WMI programmatically to create a system report. This system report contains the name and state of each Windows Service, the connection name and username for each network connection, the name and serial number of each virtual device driver, the product name of every network adapter and the name and process ID of every process currently executing in the Windows operating system. You use ManagementObjectSearcher to query the CIM using the get method of the ManagementObjectSearcher instance named MOS.

C# Code

ManagementObjectSearcher MOS;

 

MOS = new ManagementObjectSearcher("\\root\\CIMV2""SELECT Name,State FROM Win32_Service");

foreach (ManagementObject mservice in MOS.Get())

{

    Console.WriteLine("Service Name:{0}- Service State ", mservice["Name"], mservice["State"].ToString());

}

MOS = new ManagementObjectSearcher("\\root\\CIMV2""SELECT Name,UserName FROM Win32_NetworkConnection");

foreach (ManagementObject mservice in MOS.Get())

{

    Console.WriteLine("Network Connection:{0}- Connected Username ", mservice["Name"], mservice["UserName"].ToString());

}

MOS = new ManagementObjectSearcher("\\root\\CIMV2""SELECT Name,SerialNumber FROM Win32_DriverVXD");

foreach (ManagementObject mservice in MOS.Get())

{

    Console.WriteLine("Driver:{0}- Serial Number ", mservice["Name"], mservice["SerialNumber"].ToString());

}

MOS = new ManagementObjectSearcher("\\root\\CIMV2""SELECT Name,ProcessID FROM Win32_Process");

foreach (ManagementObject mservice in MOS.Get())

{

    Console.WriteLine("Process:{0}- ID ", mservice["Name"], mservice["ProcessID"].ToString());

}

MOS = new ManagementObjectSearcher("\\root\\CIMV2""SELECT ProductName FROM Win32_NetworkAdapter");

foreach (ManagementObject mservice in MOS.Get())

{

    Console.WriteLine("NetWork Adapter" + mservice["ProductName"].ToString());

}

use ManagementObjectSearcher class with query strings

the following code sample shows ho to retrieve an enumeration fo paused service using the ManagementObjectSearcher class programmatically. In  this code samo;e, u create a new instance of ManagementObjectSearcher named MOS with the scope of root\CIMV2 and the query "Select * From Win32_Service Sate="'Paused'"  that finds paused services. You then loop through the results of the query and display the paused services that are found ManagementObjectSearcher MOS;
        

MOS = new ManagementObjectSearcher("\\root\\CIMV2""SELECT * FROM Win32_Service WHERE State='Paysed'");

foreach (ManagementObject mservice in MOS.Get())

{

    Console.WriteLine("Service Name:{0}- Service Caption ", mservice["Caption "], mservice["State"].ToString());

}

Summery

In the preceding explanation we learned how to retrieve system resource information using the ManagementObjectSearcher class.


Similar Articles