Home
Manage Your Code
Snippet: Export list of Windows Services via WMI Query (C#)
Title: Export list of Windows Services via WMI Query Language: C#
Description: Creates a console app that exports a CSV formatted list of Windows Services and thier details Views: 433
Author: Garth Sweet Date Added: 3/20/2008
Copy Code  
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Management;
5using System.Net;
6
7namespace ExportServices
8{
9    // Garth Sweet: This code is public domain. 
10
11    // To use this source
12    // -----------------------
13    // Create a new .Net C# console project called "ExportServices"
14    // Go to program.cs and paste this code overtop all the code that's there. 
15    // Add a reference to System.Management
16    // Compile and voila the utility
17    // * Can be compiled for .Net 2.0, .Net 3.0, .Net 3.5 + (x86 or x64)
18    
19    class Program
20    {
21        static void Main(string[] args)
22        {
23            bool brief = false;
24            bool help = false;
25            string targetComputer = "";
26
27            // Clumsy arg checker lots of room for improvement
28            foreach(string arg in args)
29            {
30                if (arg.ToLower() == "/b")
31                    brief = true;
32                else if (arg == "/?")
33                    help = true;
34                else if (arg.StartsWith("/")) // False arg
35                    help = true;
36                else
37                    targetComputer = arg;
38            }
39
40            // Help request 
41            if (help)
42            {
43                Console.WriteLine("Export Services - Display a list of services installed on the local or a remote computer");
44                Console.WriteLine("");
45                Console.WriteLine("ExportServices ([ComputerName] [/b]) | [/?]");
46                Console.WriteLine("[ComputerName] - The name or IP of the target computer to scan");
47                Console.WriteLine("                 (Run without a computer name to scan local computer)");
48                Console.WriteLine("[/b]           - Brief output (name, caption, state)");
49                Console.WriteLine("[/?]           - Display this help");
50                Console.WriteLine("");
51                Console.WriteLine("Usage sample: ExportServices SomeComputer >Results.csv");
52                Console.WriteLine("");
53            }
54
55            // Perform the scan (via WMI)
56            else
57            {
58                // See if local scan
59                if (targetComputer.Trim().Length == 0)
60                    targetComputer = Dns.GetHostName();
61                
62                // Setup for the scan 
63                ObjectGetOptions searchOptions = null;
64                ManagementScope searchScope = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", targetComputer));
65
66                // Declare the service management class that will return the list of services installed
67                ManagementClass servicesMC = new ManagementClass(searchScope,
68                                                                 new ManagementPath("Win32_Service"),
69                                                                 searchOptions);
70
71                // Loop through each service returned from the target computer. 
72                bool first = true;
73                foreach (ManagementObject service in servicesMC.GetInstances())
74                {
75                    // Write header
76                    if (first)
77                    {
78                        if (brief)
79                            Console.WriteLine("Name,Caption,State");  
80                        else
81                            Console.WriteLine("Name,Caption,State,Started,StartMode,StartName,PathName,Description");  
82
83                        first = false;
84                    }
85
86                    Console.Write(service.GetPropertyValue("Name") + ",");
87                    Console.Write(service.GetPropertyValue("Caption") + ",");
88                    Console.Write(service.GetPropertyValue("State"));
89                    if (!brief)
90                    {
91                        Console.Write("," + service.GetPropertyValue("Started") + ",");
92                        Console.Write(service.GetPropertyValue("StartMode") + ",");
93                        Console.Write(service.GetPropertyValue("StartName") + ",");
94                        Console.Write(service.GetPropertyValue("PathName") + ",");
95                        Console.Write("\"" + service.GetPropertyValue("Description") + "\""); // Quoted for possible commas
96                    }
97                    Console.WriteLine("");
98                }
99            }
100        }
101    }
102}
103
Usage
See comments. Ideas from http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/other/wmi/
Notes
Email Bitmugger@gmail.com with comments/questions