Guest User

Untitled

a guest
Mar 9th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. namespace Teste
  2. {
  3. public partial class Form1 : Form
  4. {
  5. public Form1()
  6. {
  7. InitializeComponent();
  8.  
  9. }
  10.  
  11. private void button3_Click(object sender, EventArgs e)
  12. {
  13. List<server> listServer = new List<server>();
  14. server server01 = new server("usuario", "senha", "10.1.3.230", "Themes");
  15. server server02 = new server("usuario", "senha", "10.1.4.230", "Themes");
  16. listServer.Add(server01);
  17. listServer.Add(server02);
  18. foreach (server c in listServer) {
  19.  
  20. //passando as informações para o EnumServices - IP - Usuario - Senha - NomeServiço
  21. EnumServices(c.GetSetServerAddress, c.GetSetServerUser, c.GetSetServerPassword, c.GetSetServerService);
  22.  
  23. }
  24.  
  25. //EnumServices("10.1.4.230", "usuario", "senha", "Themes");
  26. //EnumServices("10.1.3.230", "usuario", "senha", "Themes");
  27.  
  28. }
  29.  
  30. public void ListPopulation(string serviceName, string serviceStatus, string serverAdress, string serverName) {
  31.  
  32. listView1.BeginUpdate();
  33. //cria um array com as informações para serem colocadas na colunas subsequentes.
  34. string[] row = { serviceStatus, serverAdress, serverName };
  35. listView1.Items.Add(serviceName).SubItems.AddRange(row);
  36. listView1.EndUpdate();
  37.  
  38. }
  39.  
  40. public void EnumServices(string host, string username, string password, string serviceName)
  41. {
  42. //querys de acesso e checagem *NÃO ALTERAR*
  43. string ns = @"rootcimv2";
  44. string query = "select * from Win32_Service";// where name = '" + serviceName + "'";
  45.  
  46. //define as opções e valores para a conexão
  47. ConnectionOptions options = new ConnectionOptions();
  48. if (!string.IsNullOrEmpty(username))
  49. {
  50. options.Username = username;
  51. options.Password = password;
  52. options.Impersonation = ImpersonationLevel.Impersonate;
  53. options.Authentication = AuthenticationLevel.Packet;
  54. options.EnablePrivileges = true;
  55. }
  56.  
  57. //efetua a conexão
  58. ManagementScope scope = new ManagementScope(string.Format(@"\{0}{1}", host, ns), options);
  59. scope.Connect();
  60.  
  61. //executa a query e lista os serviços
  62. ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, new ObjectQuery(query));
  63. ManagementObjectCollection retObjectCollection = searcher.Get();
  64. foreach (ManagementObject service in retObjectCollection.OfType<ManagementObject>())
  65. {
  66.  
  67. /*envia as informações obtidas para a classe que popula a lista
  68. ListPopulation(
  69. service.GetPropertyValue("Name").ToString(), //service name
  70. service.GetPropertyValue("State").ToString(), //Service State
  71. host, //server adress
  72. service.GetPropertyValue("SystemName").ToString()//server name
  73. );*/
  74.  
  75. //mostra as opções do service, não apagar
  76. Console.WriteLine(service.GetText(TextFormat.Mof));
  77. }
  78.  
  79. }
  80.  
  81. }
  82.  
  83. public class server
  84. {
  85.  
  86. private string serverPassword;
  87. private string serverUser;
  88. private string serverAddress;
  89. private string serverService;
  90.  
  91.  
  92. //construtor do objeto Server
  93. public server(string sUser, string sPassword, string sAddress, string sService)
  94. {
  95.  
  96. serverUser = sUser;
  97. serverAddress = sAddress;
  98. serverService = sService;
  99. serverPassword = sPassword;
  100.  
  101.  
  102. }
  103.  
  104.  
  105. //getters and setters
  106. public string GetSetServerPassword
  107. {
  108. get
  109. {
  110. return serverPassword;
  111. }
  112. set
  113. {
  114. serverPassword = value;
  115. }
  116.  
  117. }
  118.  
  119. public string GetSetServerAddress
  120. {
  121. get
  122. {
  123. return serverAddress;
  124. }
  125. set
  126. {
  127. serverAddress = value;
  128. }
  129.  
  130. }
  131.  
  132. public string GetSetServerUser
  133. {
  134. get
  135. {
  136. return serverUser;
  137. }
  138. set
  139. {
  140. serverUser = value;
  141. }
  142.  
  143. }
  144.  
  145. public string GetSetServerService
  146. {
  147. get
  148. {
  149. return serverService;
  150. }
  151. set
  152. {
  153. serverService = value;
  154. }
  155.  
  156. }
  157.  
  158. public override string ToString()
  159. {
  160. return base.ToString();
  161. }
  162. }
Add Comment
Please, Sign In to add comment