C# .NET - Windows Services - Remote Machine

Asked By BALAJI KRISHNAN on 02-Dec-04 07:11 AM
Hi All,
I need create a windows service which will monitor the status of  
another working service on remote machines, can anyone pls send me the sample code  to do this.
Thanks in advance,
Balaji

WMI queries

Asked By Peter Bromberg on 02-Dec-04 09:31 AM
example:
using System;
using System.Management;  
namespace WMITest
{
	class clsMain
	{
		static void Main(string[] args)
		{           
			GetWMIStats();
			Console.ReadLine();
		}
		static void GetWMIStats()
		{
			//Connection credentials to the remote computer - 
			//logged in account  must have adminstrator access for a remote machine
			ConnectionOptions oConn = new ConnectionOptions();
			//oConn.Username = "username";
			//oConn.Password = "password";
			string remoteMachineName="localhost";
			System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\" +remoteMachineName , oConn);      
			 // pass the name of the service to query with this particular WQL query---
			System.Management.RelatedObjectQuery oQuery = 
				new System.Management.RelatedObjectQuery("Win32_Service.Name='Alerter'");	 
			ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);
			ManagementObjectCollection oReturnCollection = oSearcher.Get();   
			foreach( ManagementObject oReturn in oReturnCollection ) 
				{				 
				Console.WriteLine("Name : " + oReturn["Name"].ToString());
				// Repeat the above process for each property of the respective
				// object that can be read. See the WMI documentation at MSDN for details.		 
				}   						
		}		 
	}
}

Peter, Regarding Windows Services -Remote Machine

Asked By BALAJI KRISHNAN on 03-Dec-04 01:05 AM
When I tried your code, I cannot add 
using System.Managment namespace to my project, do I need to add any reference to my project,if so please tell me what to add.
Thanks
Balaji

Windows Services DOMAIN ISSUE

Asked By BALAJI KRISHNAN on 08-Dec-04 03:40 AM
Hi All,
I have a doubt on windows services in remote machine.
I have a windows service in my machine and this service will check the status of another windows service in another machine.Both the machines are in same domain.
This is the code I used to get the services that are running in the remote machine
ServiceController[] services;
services = ServiceController.GetServices(remotemachinename);
if (services[i].ServiceName == this.servicename.ToString().Trim()) 
{
ServiceController sc = new ServiceController(services[i].ServiceName,remotemachinename); 
if(sc.Status == ServiceControllerStatus.Stopped)
{
Console.WriteLine("Service Stopped");
}
}
Now when I tried this locally(the machine whice are in same domain, it is working, I can check the status of the service.
BUT I WANT TO KNOW HOW DO I GO WITH THE MACHINE WHICH ARE IN DIFFERENT DOMAIN.
please reply
Thanks in advance,
Balaji
check this up it will help u out
Sanjiv Aggarwal replied to BALAJI KRISHNAN on 18-Jul-09 03:33 AM

try

{

bool connected = false;

ConnectionOptions cred = new ConnectionOptions();

cred.Username = ConfigurationManager.AppSettings["RemoteMachineUserName"].ToString();

cred.Password = ConfigurationManager.AppSettings["RemoteMachinePassword"].ToString();

String hostName = Dns.GetHostByAddress(ConfigurationManager.AppSettings["RemoteMachineServiceIPAddress"].ToString()).HostName;

ManagementScope srvScope;

srvScope = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", hostName), cred);

ManagementPath win32Path = new ManagementPath("Win32_Service");

ManagementClass services;

services = new ManagementClass(srvScope, win32Path, null);

try

{

services.GetInstances();

}

catch (Exception ex)

{

srvScope = new ManagementScope(string.Format("\\\\{0}\\root\\cimv2", hostName));

services = new ManagementClass(srvScope, win32Path, null);

}

foreach (ManagementObject service in services.GetInstances())

{

if (service.GetPropertyValue("Name").ToString().ToLower().Equals(ConfigurationManager.AppSettings["ServiceName"].ToString()) && service.GetPropertyValue("State").ToString().ToLower().Equals("running"))

{

connected = true;

break;

}

}

if (connected)

return "Is Alive";

else

return "not alive";

}

catch (Exception ex)

{

return "not alive";

}

Anurag replied to BALAJI KRISHNAN on 22-Sep-10 02:27 AM
U need to add reference by Right clicking on ur project and then "add reference" then select "System.Management"