I'm using the following code.
ServiceController MyController = new ServiceController();
MyController.MachineName = server_txt.Text.Trim();
MyController.ServiceName = "Service1";
string msg = MyController.Status.ToString();
Label1.Text = msg;
This code works fine for network computers where I have access. How to change this so it works for the systems in different domains using credentials?
If you use WMI, you can set the credentials in 'ConnectionOptions'.
ConnectionOptions op = new ConnectionOptions();
op.Username = "Domain\\Domainuser";
op.Password = "password";
ManagementScope scope = new ManagementScope(@"\\Servername.Domain\root\cimv2", op);
scope.Connect();
ManagementPath path = new ManagementPath("Win32_Service");
ManagementClass services;
services = new ManagementClass(scope, path, null);
foreach (ManagementObject service in services.GetInstances())
{
if (service.GetPropertyValue("State").ToString().ToLower().Equals("running"))
{ // Do something }
}