Control Services on Remote Computer in Domain

Hey,

I need to build an application to control services on the computers in the domain.
What is the best way to do it within the domain? (Please C# only)
Is WMI the only solution? (I can't rely on Active Directory)

Bonus question:
I think of using one app like this per domain (data centers can have different domains/workgroups)
Should I use Remoting to query the 2 apps and this way have 1 UI to control all the services?
Is it possible to pass the following: Service Names, Status Service, etc..

Thanks,
breezbackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gautham JanardhanCommented:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
 
namespace ServiceManager
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.ListBox lstBxServices;
		private System.Windows.Forms.Button btnProcess;
		private System.Windows.Forms.ComboBox cmbBxServices;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Label lblStatus;
        private Button btnGetService;
        private TextBox txtBxMachineName;
 
 
		System.ServiceProcess.ServiceController FObject = null;
 
		public Form1()
		{
			InitializeComponent();
		}
 
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
 
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.panel1 = new System.Windows.Forms.Panel();
            this.lblStatus = new System.Windows.Forms.Label();
            this.btnProcess = new System.Windows.Forms.Button();
            this.cmbBxServices = new System.Windows.Forms.ComboBox();
            this.lstBxServices = new System.Windows.Forms.ListBox();
            this.txtBxMachineName = new System.Windows.Forms.TextBox();
            this.btnGetService = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.btnGetService);
            this.panel1.Controls.Add(this.txtBxMachineName);
            this.panel1.Controls.Add(this.lblStatus);
            this.panel1.Controls.Add(this.btnProcess);
            this.panel1.Controls.Add(this.cmbBxServices);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel1.Location = new System.Drawing.Point(0, 213);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(292, 96);
            this.panel1.TabIndex = 1;
            // 
            // lblStatus
            // 
            this.lblStatus.Location = new System.Drawing.Point(16, 40);
            this.lblStatus.Name = "lblStatus";
            this.lblStatus.Size = new System.Drawing.Size(264, 48);
            this.lblStatus.TabIndex = 2;
            // 
            // btnProcess
            // 
            this.btnProcess.Location = new System.Drawing.Point(208, 8);
            this.btnProcess.Name = "btnProcess";
            this.btnProcess.Size = new System.Drawing.Size(75, 23);
            this.btnProcess.TabIndex = 1;
            this.btnProcess.Click += new System.EventHandler(this.btnProcess_Click);
            // 
            // cmbBxServices
            // 
            this.cmbBxServices.Location = new System.Drawing.Point(8, 8);
            this.cmbBxServices.Name = "cmbBxServices";
            this.cmbBxServices.Size = new System.Drawing.Size(192, 21);
            this.cmbBxServices.TabIndex = 0;
            // 
            // lstBxServices
            // 
            this.lstBxServices.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lstBxServices.Location = new System.Drawing.Point(0, 0);
            this.lstBxServices.Name = "lstBxServices";
            this.lstBxServices.Size = new System.Drawing.Size(292, 212);
            this.lstBxServices.TabIndex = 2;
            this.lstBxServices.SelectedIndexChanged += new System.EventHandler(this.lstBxServices_SelectedIndexChanged);
            // 
            // txtBxMachineName
            // 
            this.txtBxMachineName.Location = new System.Drawing.Point(8, 40);
            this.txtBxMachineName.Name = "txtBxMachineName";
            this.txtBxMachineName.Size = new System.Drawing.Size(192, 20);
            this.txtBxMachineName.TabIndex = 3;
            // 
            // btnGetService
            // 
            this.btnGetService.Location = new System.Drawing.Point(208, 38);
            this.btnGetService.Name = "btnGetService";
            this.btnGetService.Size = new System.Drawing.Size(75, 23);
            this.btnGetService.TabIndex = 4;
            this.btnGetService.Text = "Start";
            this.btnGetService.Click += new System.EventHandler(this.btnGetService_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 309);
            this.Controls.Add(this.lstBxServices);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Service Manager";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);
 
		}
		#endregion
 
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
 
		private void cmbBxServices_SelectedIndexChanged(object sender, EventArgs e)
		{
			if(cmbBxServices.SelectedIndex != -1)
			{
				AccessObject(cmbBxServices.SelectedItem);
			}
		}
		void AccessObject(object Data)
		{
			object obj = Data;
			FObject = 
				(System.ServiceProcess.ServiceController)obj;
			btnProcess.Text = "";
			btnProcess.Enabled = false;
			if(FObject.Status == 
				System.ServiceProcess.ServiceControllerStatus.Running)
			{
				btnProcess.Text = "Stop";
				btnProcess.Enabled = true;
			}
			else if(FObject.Status == 
				System.ServiceProcess.ServiceControllerStatus.Stopped)
			{
				btnProcess.Text = "Start";
				btnProcess.Enabled = true;
			}
			lblStatus.Text ="Service Name :- "
				+FObject.DisplayName + " \r\n Status :- "
				+ FObject.Status.ToString();
		}
 
		private void btnProcess_Click(object sender, System.EventArgs e)
		{
			if(FObject != null)
			{
				if(FObject.Status == 
					System.ServiceProcess.ServiceControllerStatus.Running)
				{
					FObject.Stop();
				}
				else if(FObject.Status == 
					System.ServiceProcess.ServiceControllerStatus.Stopped)
				{
					FObject.Start();
				}
			}
		}
 
		private void lstBxServices_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(lstBxServices.SelectedIndex != -1)
			{
				AccessObject(lstBxServices.SelectedItem);
			}
		}
 
        private void btnGetService_Click(object sender, EventArgs e)
        {
            System.ServiceProcess.ServiceController[] FControllers =
                System.ServiceProcess.ServiceController.GetServices(txtBxMachineName.Text);
 
 
            lstBxServices.DisplayMember = "DisplayName";
            for (int i = 0; i < FControllers.Length; i++)
            {
                lstBxServices.Items.Add(FControllers[i]);
                if (FControllers[i].DisplayName.IndexOf("MSSQL", 0, 5) >= 0)
                {
                    cmbBxServices.Items.Add(FControllers[i]);
                }
            }
            cmbBxServices.DisplayMember = "DisplayName";
            cmbBxServices.ValueMember = "ServiceName";
            cmbBxServices.SelectedIndexChanged += new EventHandler(cmbBxServices_SelectedIndexChanged);
        }
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
grayeCommented:
gauthampj has the correct answer... the "native" ServiceProcess namespace can do almost everything.

The only exception is that it can't change the startup type (manual, automatic, disabled).   But you can still use WMI to perform that one task
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.