Advertisement
Guest User

Untitled

a guest
Mar 10th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. string clusterName = "app-server.local";
  2. string custerGroupResource = "cluster.server.local";
  3.  
  4. ConnectionOptions options = new ConnectionOptions
  5. {
  6. Authentication = AuthenticationLevel.PacketPrivacy
  7. };
  8.  
  9. ManagementScope s = new ManagementScope("\\" + clusterName, options);
  10.  
  11. ManagementPath p = new ManagementPath("Mscluster_Clustergroup.Name='" + custerGroupResource + "'");
  12.  
  13. using (ManagementObject clrg = new ManagementObject(s, p, null))
  14. {
  15. clrg.Get();
  16. Console.WriteLine(clrg["Status"]);
  17. }
  18.  
  19. public class ClusterManager
  20. {
  21. private readonly string ClusterName;
  22. private readonly string ClusterNamespace;
  23. private ManagementScope Scope;
  24. private readonly ConnectionOptions Options;
  25.  
  26. public ClusterManager(string clusterName, string clusterNamespace, string user, string password)
  27. {
  28. ClusterName = clusterName;
  29. ClusterNamespace = clusterNamespace;
  30.  
  31. Options = new ConnectionOptions
  32. {
  33. Authentication = AuthenticationLevel.PacketPrivacy,
  34. Username = user,
  35. Password = password
  36. };
  37. }
  38.  
  39. public void Connect()
  40. {
  41. Scope = new ManagementScope($@"\{ClusterName}root{ClusterNamespace}", Options);
  42. Scope.Connect();
  43. }
  44.  
  45. public ManagementObject GetResource(string name)
  46. {
  47. string wmiClassName = "MSCluster_Resource";
  48.  
  49. ManagementClass managementClass = new ManagementClass(Scope, new ManagementPath(wmiClassName), null);
  50. managementClass.Get();
  51. ManagementObjectCollection objectCollection = managementClass.GetInstances();
  52.  
  53. foreach (ManagementBaseObject obj in objectCollection)
  54. {
  55. ManagementObject resource = (ManagementObject) obj;
  56.  
  57. if (resource["Name"].ToString() == name)
  58. {
  59. return resource;
  60. }
  61. }
  62.  
  63. return null;
  64. }
  65.  
  66. public void TakeOffline(ManagementObject resource)
  67. {
  68. resource.InvokeMethod("Takeoffline", null, null);
  69. }
  70.  
  71. public void BringOnline(ManagementObject resource)
  72. {
  73. resource.InvokeMethod("Bringonline", null, null);
  74. }
  75. }
  76.  
  77. using System;
  78. using System.Collections.Generic;
  79. using System.Linq;
  80. using System.Text;
  81. using System;
  82. using System.Management;
  83.  
  84. namespace MyNamespace
  85. {
  86. public class ClusterAdmin
  87. {
  88. [MTAThread]
  89. public static void Main()
  90. {
  91. string clusterName = "MyCluster"; // cluster alias
  92. string custerGroupResource = "FS_Resource1"; // Cluster group name
  93. ConnectionOptions options = new ConnectionOptions();
  94. options.Username = "ClusterAdmin"; //could be in domainuser format
  95. options.Password = "HisPassword";
  96.  
  97. // Connect with the mscluster WMI namespace on the cluster named "MyCluster"
  98. ManagementScope s = new ManagementScope("\\" + clusterName + "\root\mscluster", options);
  99. ManagementPath p = new ManagementPath("Mscluster_Clustergroup.Name='" + custerGroupResource + "'");
  100.  
  101. using (ManagementObject clrg = new ManagementObject(s, p, null))
  102. {
  103. // Take clustergroup off line and read its status property when done
  104. TakeOffLine(clrg);
  105. clrg.Get();
  106. Console.WriteLine(clrg["Status"]);
  107.  
  108. System.Threading.Thread.Sleep(3000); // Sleep for a while
  109.  
  110. // Bring back online and get status.
  111. BringOnLine(clrg);
  112. clrg.Get();
  113. Console.WriteLine(clrg["Status"]);
  114. }
  115. }
  116. static void TakeOffLine(ManagementObject resourceGroup)
  117. {
  118. ManagementBaseObject outParams =
  119. resourceGroup.InvokeMethod("Takeoffline", null, null);
  120. }
  121. static void BringOnLine(ManagementObject resourceGroup)
  122. {
  123. ManagementBaseObject outParams =
  124. resourceGroup.InvokeMethod("Takeoffline", null, null);
  125. }
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement