How to Get a List of Services Using the SDK
Published Feb 15 2019 06:51 AM 1,458 Views
First published on TECHNET on May 25, 2011

Customer asks – how can I get a list of services using the SDK?  Easy – less than 10 lines of code.


Here is a simple console application to demonstrate.


First create a new application (or use an application you already have going) and add references to the three SDK DLLs in the C:\Program Files\Microsoft System Center\Service Manager 2010\SDK Binaries directory.



Next add some using statements and some code to the Main().


This example is really simple.  You just need to create a connection to the server by creating an EnterpriseManagementGroup.  Then get the System.Library management pack.  Then get the System.Service class inside of that MP.  Then create IObjectReader by passing the service class to get all of the service objects that you can then loop through and do whatever you need.


using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;
namespace GetServices
{
class Program
{
static void Main(string[] args)
{
EnterpriseManagementGroup emg = new EnterpriseManagementGroup("localhost");
ManagementPack mpSystem = emg.ManagementPacks.GetManagementPack(SystemManagementPack.System);
ManagementPackClass classService = mpSystem.GetClass("System.Service");
IObjectReader<EnterpriseManagementObject> readerServices = emg.EntityObjects.GetObjectReader<EnterpriseManagementObject>(classService, ObjectQueryOptions.Default);
foreach (EnterpriseManagementObject emoService in readerServices)
{
Console.WriteLine(emoService.DisplayName);
}
Console.Read();
}
}
}
Results in this:

Note: I just created a set of 10 test services using SMLets numbered 1 through 10 plus the out of the box service called ‘System Center Management Group’.
Visual Studio project attached.
While we are on the subject here is how you can do the same thing with SMLets:

GetServices.zip

Version history
Last update:
‎Mar 11 2019 08:47 AM
Updated by: