Guest User

Untitled

a guest
Jun 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. static void EnumServices(string host, string username, string password)
  2. {
  3. string ns = @"rootcimv2";
  4. string query = "select * from Win32_Service";
  5.  
  6. ConnectionOptions options = new ConnectionOptions();
  7. if (!string.IsNullOrEmpty(username))
  8. {
  9. options.Username = username;
  10. options.Password = password;
  11. }
  12.  
  13. ManagementScope scope =
  14. new ManagementScope(string.Format(@"\{0}{1}", host, ns), options);
  15. scope.Connect();
  16.  
  17. ManagementObjectSearcher searcher =
  18. new ManagementObjectSearcher(scope, new ObjectQuery(query));
  19. ManagementObjectCollection retObjectCollection = searcher.Get();
  20. foreach (ManagementObject mo in searcher.Get())
  21. {
  22. Console.WriteLine(mo.GetText(TextFormat.Mof));
  23. }
  24. }
  25.  
  26. namespace AtYourService
  27. {
  28. using System;
  29. using System.ServiceProcess;
  30.  
  31. class Program
  32. {
  33. static void Main(string[] args)
  34. {
  35. ServiceController[] services = ServiceController.GetServices();
  36.  
  37. foreach (ServiceController service in services)
  38. {
  39. Console.WriteLine(
  40. "The {0} service is currently {1}.",
  41. service.DisplayName,
  42. service.Status);
  43. }
  44.  
  45. Console.Read();
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment