Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. public static void RestartComputers()
  2. {
  3. ManagementObjectCollection instances;
  4. List<string> ComputerNames = GetComputers();
  5. foreach (var i in ComputerNames)
  6. {
  7. try
  8. {
  9. string computerPath = string.Format(@"\{0}rootcimv2", i);
  10. ConnectionOptions options = new ConnectionOptions();
  11. options.Impersonation = System.Management.ImpersonationLevel.Impersonate;
  12. //options.EnablePrivilages = true;
  13. //or
  14. //options.Username = "username";
  15. //options.Password = "password";
  16. //options.Authority = "";
  17. ManagementScope scope = new ManagementScope(computerPath, options);
  18. scope.Connect();
  19.  
  20. ManagementPath osPath = new ManagementPath("Win32_OperatingSystem");
  21. ManagementClass os = new ManagementClass(scope, osPath, null);
  22. instances = os.GetInstances();
  23.  
  24.  
  25. }
  26. catch (Exception)
  27. {
  28.  
  29. throw;
  30. }
  31.  
  32. foreach (ManagementObject instance in instances)
  33. {
  34. object result = instance.InvokeMethod("Reboot", new object[] { });
  35. uint returnValue = (uint)result;
  36.  
  37. if (returnValue != 0)
  38. {
  39. throw new Exception();
  40. }
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement