Beispiel #1
0
        private bool RunCommand(WindowsMachine machine, WindowsService service, string command, ManagementBaseObject inParams = null)
        {
            var objPath  = string.Format("select * from Win32_Service where DisplayName='{0}'", service.Name);
            var scope    = ConnectToMachine(machine);
            var observer = new ManagementOperationObserver();

            var searcher = new ManagementObjectSearcher(scope, new ObjectQuery(string.Format(objPath)));

            _isOperationCompleted = false;
            observer.ObjectReady += ObjectTobeReturned;
            //observer.Completed += Completed;
            //observer.Progress += Progress;
            foreach (ManagementObject mo in searcher.Get())
            {
                mo.InvokeMethod(observer, command, inParams, null);
            }


            var sleepCount = 0;

            while (!_isOperationCompleted && sleepCount <= 60)
            {
                Thread.Sleep(1000);
                sleepCount++;
            }
            return(_isOperationCompleted);
        }
        private void lstServers_SelectedIndexChanged(object sender, EventArgs e)
        {
            grpManageService.Enabled = lstServers.SelectedItems.Count > 0;
            btnDeleteMachine.Enabled = lstServers.SelectedItems.Count > 0;
            if (lstServers.SelectedItems.Count > 0)
            {
                _selectedMachine = _machines.First(x => x.Name.Equals(lstServers.SelectedItem.ToString()));
                IWindowsServiceManager windowsServiceManager = null;
                if (_selectedMachine.IsWmiEnabled)
                {
                    windowsServiceManager = new WMIWindowServiceManager();

                    dgvManageServices.DataSource = bsDgvServices;
                    bsDgvServices.DataSource     = windowsServiceManager.GetServices(_selectedMachine);
                    var dataGridViewColumn = dgvManageServices.Columns["Machine"];
                    if (dataGridViewColumn != null)
                    {
                        dataGridViewColumn.Visible = false;
                    }
                    var gridViewColumn = dgvManageServices.Columns["IsSelected"];
                    if (gridViewColumn != null)
                    {
                        gridViewColumn.Visible = false;
                    }
                }
                else
                {
                    MessageBox.Show("Information", "WMI Not Enabled in the machine");
                }
            }
            else
            {
                bsDgvServices.DataSource = new List <WindowsService>();
            }
        }
Beispiel #3
0
 public WindowsService StopService(WindowsMachine machine, WindowsService service)
 {
     if (RunCommand(machine, service, "StopService"))
     {
         service.Status = ServiceStatus.Stopped;
     }
     return(service);
 }
Beispiel #4
0
        public List <string> ListServices(WindowsMachine machine, string serviceNameLike = "%")
        {
            const string basequery = "select * from Win32_Service";
            var          scope     = ConnectToMachine(machine);
            var          query     = string.Format("{0} where DisplayName like '{1}'", basequery, serviceNameLike);
            var          searcher  =
                new ManagementObjectSearcher(scope, new ObjectQuery(string.Format(query)));

            return((from ManagementObject mo in searcher.Get() select mo["DisplayName"].ToString()).OrderBy(x => x).ToList());
        }
Beispiel #5
0
 public bool CanTheMachineBeFound(WindowsMachine machine)
 {
     try
     {
         ConnectToMachine(machine);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Beispiel #6
0
 public WindowsService RestartService(WindowsMachine machine, WindowsService service)
 {
     if (RunCommand(machine, service, "StopService"))
     {
         service.Status = ServiceStatus.Stopped;
         var i = 0;
         while (!GetService(machine, service.Name).Status.Equals(ServiceStatus.Stopped) && i < 60)
         {
             Thread.Sleep(1000);
             i++;
         }
         if (RunCommand(machine, service, "StartService"))
         {
             service.Status = ServiceStatus.Started;
         }
     }
     return(service);
 }
Beispiel #7
0
        public WindowsService GetService(WindowsMachine machine, string serviceName)
        {
            const string basequery = "select * from Win32_Service";
            var          scope     = ConnectToMachine(machine);
            var          query     = string.Format("{0} where DisplayName = '{1}'", basequery, serviceName);
            var          searcher  =
                new ManagementObjectSearcher(scope, new ObjectQuery(string.Format(query)));
            var mo = searcher.Get();

            return((from ManagementBaseObject service in mo
                    select new WindowsService()
            {
                Name = service["DisplayName"].ToString(),
                Status = ServiceStatusMapping[service["State"].ToString()],
                StartUpType = StartTypeMapping[service["StartMode"].ToString()],
                Machine = machine,
                IsSelected = machine.Services.Any(x => x.Name.Equals(service["DisplayName"].ToString())) ? machine.Services.First(x => x.Name.Equals(service["DisplayName"].ToString())).IsSelected : false
            }).FirstOrDefault());
        }
Beispiel #8
0
        private static ManagementScope ConnectToMachine(WindowsMachine machine)
        {
            const string ns = @"root\cimv2";

            var options = new ConnectionOptions();

            if (!machine.Name.Equals("."))
            {
                if (!string.IsNullOrEmpty(machine.UserName))
                {
                    options.Username = machine.UserName;
                    options.Password = machine.Password;
                }
            }

            var scope =
                new ManagementScope(string.Format(@"\\{0}\{1}", machine.Name, ns), options);

            scope.Connect();

            return(scope);
        }
Beispiel #9
0
        public WindowsService DoOperation(ServiceOperation operation, WindowsMachine machine, WindowsService service)
        {
            switch (operation)
            {
            case ServiceOperation.Continue:
                return(ResumeService(machine, service));

            case ServiceOperation.Pause:
                return(PauseService(machine, service));

            case ServiceOperation.Restart:
                return(RestartService(machine, service));

            case ServiceOperation.Start:
                return(StartService(machine, service));

            case ServiceOperation.Stop:
                return(StopService(machine, service));

            default:
                throw new InvalidEnumArgumentException(string.Format("{0} is invalid", operation));
            }
        }
 private void btnFindMachine_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(txtMachine.Text.Trim()))
     {
         _selectedMachine = new WindowsMachine {
             Name = txtMachine.Text.Trim(), IsWmiEnabled = chkIsWmi.Checked
         };
         if (!txtMachine.Text.Trim().Equals("."))
         {
             var logon = new frmLogon(_selectedMachine);
             logon.ShowDialog();
         }
         else
         {
             CommonPlace.IsMachineFound = true;
         }
         chkMachineFound.Checked = CommonPlace.IsMachineFound;
     }
     else
     {
         MessageBox.Show("Machine name Cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Beispiel #11
0
        public WindowsService UpdatedStartupType(WindowsMachine machine, WindowsService service, ServiceStartupType serviceStartupType)
        {
            var objPath  = string.Format("select * from Win32_Service where DisplayName='{0}'", service.Name);
            var scope    = ConnectToMachine(machine);
            var observer = new ManagementOperationObserver();

            var searcher = new ManagementObjectSearcher(scope, new ObjectQuery(string.Format(objPath)));

            _isOperationCompleted = false;
            observer.ObjectReady += ObjectTobeReturned;


            //observer.Completed += Completed;
            //observer.Progress += Progress;
            foreach (ManagementObject mo in searcher.Get())
            {
                ManagementBaseObject inParams = mo.GetMethodParameters("ChangeStartMode");
                inParams["StartMode"] = StartTypeMapping.First(x => x.Value.Equals(serviceStartupType));

                mo.InvokeMethod(observer, "ChangeStartMode", inParams, null);
            }


            var sleepCount = 0;

            while (!_isOperationCompleted && sleepCount <= 60)
            {
                Thread.Sleep(1000);
                sleepCount++;
            }
            if (_isOperationCompleted)
            {
                service.StartUpType = serviceStartupType;
            }
            return(service);
        }
Beispiel #12
0
 public frmLogon(WindowsMachine machine)
 {
     _machine = machine;
     InitializeComponent();
 }
 public frmListServices(WindowsMachine machine, string serviceNameLike = "%")
 {
     _machine         = machine;
     _serviceNameLike = serviceNameLike;
     InitializeComponent();
 }