Monday 3 August 2020

Start Stop Window Service of Remote Machine

using System;
using System.Drawing;
using System.Management;
using System.Web.Configuration;
using System.Web.UI;

namespace WebApplication2_winservice
{
    public partial class _Default : Page
    {
        string serviceName = WebConfigurationManager.AppSettings["ServiceName"].ToString();
        string machineName = WebConfigurationManager.AppSettings["MachineName"].ToString();
        string userName = WebConfigurationManager.AppSettings["UserName"].ToString();
        string password = WebConfigurationManager.AppSettings["Password"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StatusShow();
            }
        }
        private void StatusShow()
        {
            string serviceStatus = Convert.ToString(ViewState["Status"]);
            if (serviceStatus != "")
            {
                if (serviceStatus == "Running")
                {
                    LblServiceStatus.ForeColor = Color.Green;
                    LblServiceStatus.Text = "Deploy Service is Running!";
                }
                else
                {
                    LblServiceStatus.ForeColor =Color.Red;
                    LblServiceStatus.Text = "Deploy Service is Stopped!";
                }

            }
            else if (ServiceStatus())
            {
                LblServiceStatus.ForeColor = Color.Green;
                LblServiceStatus.Text = "Deploy Service is Running!";
            }
            else
            {
                LblServiceStatus.ForeColor = Color.Red;
                LblServiceStatus.Text = "Deploy Service is Stopped!";
            }
        }
        private void StartStopService()
        {         
            ConnectionOptions connectoptions = new ConnectionOptions();
            connectoptions.Username = userName;
            connectoptions.Password = password;

            //IP Address of the remote machine
            ManagementScope scope = new ManagementScope(@"\\" + machineName + @"\root\cimv2");
            scope.Options = connectoptions;

            //WMI query to be executed on the remote machine
            SelectQuery query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'");

            using (ManagementObjectSearcher searcher = new
                        ManagementObjectSearcher(scope, query))
            {
                ManagementObjectCollection collection = searcher.Get();
                foreach (ManagementObject service in collection)
                {

                    if (service["Started"].Equals(true))
                    {
                        //Stop the service                       
                        service.InvokeMethod("StopService", null);
                        ViewState["Status"] = "Running";
                    }
                    if (service["Started"].Equals(false))
                    {
                        //Start the service                     
                        service.InvokeMethod("StartService", null);
                        ViewState["Status"] = "Stopped";
                    }

                }
            }
            Response.Redirect("Default.aspx");
        }
        private bool ServiceStatus()
        {
            bool status = false;
           
            ConnectionOptions connectoptions = new ConnectionOptions();
            connectoptions.Username = userName;
            connectoptions.Password = password;

            //IP Address of the remote machine
            ManagementScope scope = new ManagementScope(@"\\" + machineName + @"\root\cimv2");
            scope.Options = connectoptions;

            //WMI query to be executed on the remote machine
            SelectQuery query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'");

            using (ManagementObjectSearcher searcher = new
                        ManagementObjectSearcher(scope, query))
            {
                ManagementObjectCollection collection = searcher.Get();
                foreach (ManagementObject service in collection)
                {

                    if (service["Started"].Equals(true))
                    {
                        status = true;
                    }
                    if (service["Started"].Equals(false))
                    {
                        status = false;
                    }
                }
            }
            return status;
        }
        protected void btnStartStop_Click(object sender, EventArgs e)
        {
            StartStopService();
        }
    }
}

No comments:

Post a Comment