I am trying to create an application where I can stop and start specific windows services on a remote computer. I am using Microsoft.Management.Infrastructure to connect to WMI remotely in C#. Right now I am able to retrieve the service, but how do I invoke the StopService or StartService method using my QueryInstance?
CimCredential cimCredential = new CimCredential(PasswordAuthenticationMechanism.Default, ".", "<userName>", secureString);
WSManSessionOptions wSManSessionOptions = new WSManSessionOptions();
wSManSessionOptions.AddDestinationCredentials(cimCredential);
CimSession cimSession = CimSession.Create("<IPAddress>", wSManSessionOptions);
IEnumerable < CimInstance> queryInstance = cimSession.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_Service WHERE Name = '<ServiceName>'");
UPDATE: CimSession.InvokeMethod is how it is done. The code for stopping and starting is below
List<CimInstance> serviceList = queryInstance.ToList();
CimInstance service = serviceList[0];
cimSession.InvokeMethod(service, "StopService", null);
Thread.Sleep(15000);
cimSession.InvokeMethod(service, "StartService", null);
Thanks to H.G. Sandhagen's comment the correct way is to use CimSession.InvokeMethod. See update in question for what it looks like.