linq - C# Iterating and Comparing/Identifying WMI Services Effectively -


i working on querying specific services on remote server in attempt determine status of services, , starting/stopping/pausing them accordingly.

a snippet of server object class follows:

public class server {     public managementclass services { get; set; }      public list<string> targetservices { get; set; } } 

i make use of managementclass object connect server sort of suite of other necessities (scope, connectionoptions, , managementpath("win32_service"). targetedservices small list of services have defined intend target within services returned in connection results. example of targetedservice can seen follows:

    server.targetedservices = new list<string>() { "servicea", "serviceb" }; 

now, struggles come play when wish find, available services, services have defined in small list.. purposes of isolating them , manipulating them. have accomplished task, processing running terribly slow. hoping find clever, streamline solution this. thoughts?

here current (cringe) logic:

    public void pingservices(list<server> servers)     {         foreach(server server in servers)         {             foreach(managementobject service in server.services.getinstances())             {                 foreach(string target in server.targetservices)                 {                     if(service.getpropertyvalue("name").tostring() == target)                     {                         service.invokemethod("startservice", null);                     }                 }             }         }     } 

i've solved this. understand instead of returning instances in collection not comparable/filterable strings, i'm querying want wql (not sql-- annoying, no in operator) statement, filtering results @ server before returned. performance has increased notably.

string sql = string.format("select * win32_service name = {0}", string.join(" or name =", server.targetservices));  managementobjectsearcher searcher = new managementobjectsearcher(server.services.scope, new objectquery()                 {                     querystring = sql                 });              foreach (managementobject service in searcher.get())             {                 service.invokemethod("startservice", null);                 //my target services             } 

Comments