How to check more than one service using WMI Query or C #?

0

I have this project where the system will check for specific services on several different remote machines.

All of the connection, network and etc are defined and working, but I still have not been able to figure out a way to get the query to check another service efficiently.

Here is the code:

namespace Teste
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button3_Click(object sender, EventArgs e)
        {
            List<server> listServer = new List<server>();
            server server01 = new server("usuario", "senha", "10.1.3.230", "Themes");
            server server02 = new server("usuario", "senha", "10.1.4.230", "Themes");
            listServer.Add(server01);
            listServer.Add(server02);
            foreach (server c in listServer) {

                //passando as informações para o EnumServices - IP - Usuario - Senha - NomeServiço
                EnumServices(c.GetSetServerAddress, c.GetSetServerUser, c.GetSetServerPassword, c.GetSetServerService);

            }

            //EnumServices("10.1.4.230", "usuario", "senha", "Themes");
            //EnumServices("10.1.3.230", "usuario", "senha", "Themes");

        }

        public void ListPopulation(string serviceName, string serviceStatus, string serverAdress, string serverName) {

            listView1.BeginUpdate();
            //cria um array com as informações para serem colocadas na colunas subsequentes.
            string[] row = { serviceStatus, serverAdress, serverName };
            listView1.Items.Add(serviceName).SubItems.AddRange(row);
            listView1.EndUpdate();

        }

        public void EnumServices(string host, string username, string password, string serviceName)
        {
            //querys de acesso e checagem *NÃO ALTERAR*
            string ns = @"root\cimv2";
            string query = "select * from Win32_Service";// where name = '" + serviceName + "'";

            //define as opções e valores para a conexão
            ConnectionOptions options = new ConnectionOptions();
            if (!string.IsNullOrEmpty(username))
            {
                options.Username = username;
                options.Password = password;
                options.Impersonation = ImpersonationLevel.Impersonate;
                options.Authentication = AuthenticationLevel.Packet;
                options.EnablePrivileges = true;
            }

            //efetua a conexão
            ManagementScope scope = new ManagementScope(string.Format(@"\{0}\{1}", host, ns), options);
            scope.Connect();

            //executa a query e lista os serviços
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query));
            ManagementObjectCollection retObjectCollection = searcher.Get();
            foreach (ManagementObject service in retObjectCollection.OfType<ManagementObject>())
            {

                /*envia as informações obtidas para a classe que popula a lista
                ListPopulation(
                    service.GetPropertyValue("Name").ToString(), //service name
                    service.GetPropertyValue("State").ToString(), //Service State
                    host,                                          //server adress
                    service.GetPropertyValue("SystemName").ToString()//server name
                    );*/

                //mostra as opções do service, não apagar
                Console.WriteLine(service.GetText(TextFormat.Mof));
            }

        }

    }

    public class server
    {

        private string serverPassword;
        private string serverUser;
        private string serverAddress;
        private string serverService;


        //construtor do objeto Server
        public server(string sUser, string sPassword, string sAddress, string sService)
        {

            serverUser = sUser;
            serverAddress = sAddress;
            serverService = sService;
            serverPassword = sPassword;


        }


        //getters and setters
        public string GetSetServerPassword
        {
            get
            {
                return serverPassword;
            }
            set
            {
                serverPassword = value;
            }

        }

        public string GetSetServerAddress
        {
            get
            {
                return serverAddress;
            }
            set
            {
                serverAddress = value;
            }

        }

        public string GetSetServerUser
        {
            get
            {
                return serverUser;
            }
            set
            {
                serverUser = value;
            }

        }

        public string GetSetServerService
        {
            get
            {
                return serverService;
            }
            set
            {
                serverService = value;
            }

        }

        public override string ToString()
        {
            return base.ToString();
        }
    }
}

Basically this is the heart of the project, but we still need to implement checking for more than one service at a time.

    
asked by anonymous 09.03.2018 / 14:06

0 answers