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:
- 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.
- Select * from win32_networkconnection: This WQL statement returns all properties of all network connections made from a computer .
- 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.
- SELECT 8 FROM Win32_Process: This WQL statement returns all processes that are currently executing in the Windows operating system.
- 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())
{
Join the conversation! Your thoughts help the community grow.