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.
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)
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: