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>();
            }
        }
        private void brnFindService_Click(object sender, EventArgs e)
        {
            if (txtService.Text.Trim().Length <= 0)
            {
                return;
            }
            var selectedMachine = _machines.First(x => x.Name.Equals(lstServers.SelectedItem.ToString()));
            var services        = new frmListServices(selectedMachine, txtService.Text);

            services.ShowDialog();
            foreach (var service in CommonPlace.PickedUpWindowsServices)
            {
                if (_selectedMachine.Services.Any(x => x.Name.Equals(service.Name)))
                {
                    _selectedMachine.Services.Remove(_selectedMachine.Services.First(x => x.Name.Equals(service.Name)));
                }
                _selectedMachine.Services.Add(service);
            }

            var serviceNameArray = selectedMachine.Services.Select(x => x.Name).ToArray();

            selectedMachine.Services.Clear();
            for (var i = 0; i < serviceNameArray.Count(); i++)
            {
                IWindowsServiceManager windowsServiceManager = null;
                if (_selectedMachine.IsWmiEnabled)
                {
                    windowsServiceManager = new WMIWindowServiceManager();
                    var service = windowsServiceManager.GetService(_selectedMachine,
                                                                   serviceNameArray[i]);
                    if (service != null)
                    {
                        selectedMachine.Services.Add(service);
                    }
                }
                else
                {
                    MessageBox.Show("Information", "WMI Not Enabled in the machine");
                }
            }
            bsDgvServices.DataSource = selectedMachine.Services;
            //dgvManageServices.DataSource = bsDgvServices;
            var dataGridViewColumn = dgvManageServices.Columns["Machine"];

            if (dataGridViewColumn != null)
            {
                dataGridViewColumn.Visible = false;
            }
            var gridViewColumn = dgvManageServices.Columns["IsSelected"];

            if (gridViewColumn != null)
            {
                gridViewColumn.Visible = false;
            }
            dgvManageServices.ResetBindings();
        }
Beispiel #3
0
        private void RefreshServices()
        {
            if (lstServers.SelectedItems.Count > 0)
            {
                var services = new List <WindowsService>();

                foreach (var selectedMachine in _machines.Where(x => x.IsSelected))
                {
                    IWindowsServiceManager windowsServiceManager;
                    if (selectedMachine.IsWmiEnabled)
                    {
                        windowsServiceManager = new WMIWindowServiceManager();
                        services.AddRange(windowsServiceManager.GetServices(selectedMachine));
                    }
                    else
                    {
                        MessageBox.Show("Information",
                                        string.Format("WMI Not Enabled in the machine: {0}", selectedMachine.Name));
                    }
                }

                _bindingSourceDgvServices.DataSource = new BindingList <WindowsService>(services);
                var dataGridViewColumn = dgvServices.Columns["Machine"];
                if (dataGridViewColumn != null)
                {
                    dataGridViewColumn.Visible = false;
                }

                foreach (DataGridViewRow row in dgvServices.Rows)
                {
                    var selectedMachine = _machines.First(x => x.Name.Equals(row.Cells["MachineName"].Value.ToString()));
                    var serviceName     = row.Cells["Name"].Value.ToString();
                    var serviceStatus   = row.Cells["Status"].Value.ToString();
                    var selectedService = selectedMachine.Services.First(x => x.Name.Equals(serviceName));
                    selectedService.Status = (ServiceStatus)Enum.Parse(typeof(ServiceStatus), serviceStatus);
                }
            }
            else
            {
                _bindingSourceDgvServices.DataSource = new BindingList <WindowsService>(new List <WindowsService>());
            }
        }
Beispiel #4
0
 private void btnFindMachine_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(txtUsername.Text))
     {
         _machine.UserName = txtUsername.Text;
         _machine.Password = txtPassword.Text;
         IWindowsServiceManager windowsServiceManager = null;
         if (_machine.IsWmiEnabled)
         {
             windowsServiceManager = new WMIWindowServiceManager();
         }
         else
         {
             MessageBox.Show("WMI Not Enabled in the machine", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
             return;
         }
         CommonPlace.IsMachineFound = windowsServiceManager.CanTheMachineBeFound(_machine);
         this.Close();
     }
     else
     {
         MessageBox.Show("Username Cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Beispiel #5
0
        private void DoServiceOperation(ServiceOperation operation, ServiceStatus expectedStatus)
        {
            DisableStuff();
            processStatus.Value      = 0;
            lblPerogressPercent.Text = string.Format("{0}%", 0);
            var bgWorkers = new List <BackgroundWorker>();

            foreach (var selectedMachine in _machines.Where(x => x.IsSelected))
            {
                var selectedServiceCount =
                    _machines.Where(x => x.IsSelected).Sum(x => x.Services.Count(y => y.IsSelected));
                if (selectedMachine.IsWmiEnabled)
                {
                    var i = 0;
                    foreach (var selectedService in selectedMachine.Services.Where(x => x.IsSelected))
                    {
                        var bgWorkerForService = new BackgroundWorker()
                        {
                            WorkerReportsProgress      = true,
                            WorkerSupportsCancellation = true
                        };
                        if (!selectedService.Status.Equals(expectedStatus))
                        {
                            continue;
                        }
                        IWindowsServiceManager windowsServiceManager = new WMIWindowServiceManager();
                        var service = selectedService;
                        var machine = selectedMachine;

                        bgWorkerForService.ProgressChanged += (sender3, e3) =>
                        {
                            processStatus.Value = (e3.ProgressPercentage * 100) /
                                                  (2 * selectedServiceCount);
                            lblPerogressPercent.Text = string.Format("{0}%",
                                                                     processStatus
                                                                     .Value);
                        };
                        bgWorkerForService.DoWork += (sender2, e2) =>
                        {
                            bgWorkerForService.ReportProgress(i++);
                            windowsServiceManager.DoOperation(operation, machine, service);
                            bgWorkerForService.ReportProgress(i++);
                        };
                        bgWorkerForService.RunWorkerCompleted += (sender2, e2) => bgWorkerForService.Dispose();
                        bgWorkerForService.RunWorkerAsync();
                        bgWorkers.Add(bgWorkerForService);
                    }
                }
                else
                {
                    // ReSharper disable LocalizableElement
                    MessageBox.Show("Non WMI machines are not supported in this Version", "Warning",
                                    // ReSharper restore LocalizableElement
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                // bgWorkerForServices.RunWorkerAsync();
            }
            Task.Factory.StartNew(() =>
            {
                while (bgWorkers.Count(x => x.IsBusy) != 0)
                {
                }
                if (InvokeRequired)
                {
                    Invoke((Action)(() =>
                    {
                        EnableStuff();
                        RefreshServices();
                        processStatus.Value = 100;
                        lblPerogressPercent.Text = string.Format("{0}%", processStatus.Value);
                    }));
                }
            });
        }