Can C # get information about the cluster?
For example to get a list of roles and their resources?

If you can - skip the link to read how to do it.

I tried it this way, but I get System.Managment.ManagmentException: Invalid class

 string clusterName = "app-server.local"; string custerGroupResource = "cluster.server.local"; ConnectionOptions options = new ConnectionOptions { Authentication = AuthenticationLevel.PacketPrivacy }; ManagementScope s = new ManagementScope("\\\\" + clusterName, options); ManagementPath p = new ManagementPath("Mscluster_Clustergroup.Name='" + custerGroupResource + "'"); using (ManagementObject clrg = new ManagementObject(s, p, null)) { clrg.Get(); Console.WriteLine(clrg["Status"]); } 

    2 answers 2

    I post my decision. Getting resources, stopping and starting resources.

     public class ClusterManager { private readonly string ClusterName; private readonly string ClusterNamespace; private ManagementScope Scope; private readonly ConnectionOptions Options; public ClusterManager(string clusterName, string clusterNamespace, string user, string password) { ClusterName = clusterName; ClusterNamespace = clusterNamespace; Options = new ConnectionOptions { Authentication = AuthenticationLevel.PacketPrivacy, Username = user, Password = password }; } public void Connect() { Scope = new ManagementScope($@"\\{ClusterName}\root\{ClusterNamespace}", Options); Scope.Connect(); } public ManagementObject GetResource(string name) { string wmiClassName = "MSCluster_Resource"; ManagementClass managementClass = new ManagementClass(Scope, new ManagementPath(wmiClassName), null); managementClass.Get(); ManagementObjectCollection objectCollection = managementClass.GetInstances(); foreach (ManagementBaseObject obj in objectCollection) { ManagementObject resource = (ManagementObject) obj; if (resource["Name"].ToString() == name) { return resource; } } return null; } public void TakeOffline(ManagementObject resource) { resource.InvokeMethod("Takeoffline", null, null); } public void BringOnline(ManagementObject resource) { resource.InvokeMethod("Bringonline", null, null); } } 

      Try this ( source ) and do not forget to include the System.Management library in the project.

       using System; using System.Collections.Generic; using System.Linq; using System.Text; using System; using System.Management; namespace MyNamespace { public class ClusterAdmin { [MTAThread] public static void Main() { string clusterName = "MyCluster"; // cluster alias string custerGroupResource = "FS_Resource1"; // Cluster group name ConnectionOptions options = new ConnectionOptions(); options.Username = "ClusterAdmin"; //could be in domain\user format options.Password = "HisPassword"; // Connect with the mscluster WMI namespace on the cluster named "MyCluster" ManagementScope s = new ManagementScope("\\\\" + clusterName + "\\root\\mscluster", options); ManagementPath p = new ManagementPath("Mscluster_Clustergroup.Name='" + custerGroupResource + "'"); using (ManagementObject clrg = new ManagementObject(s, p, null)) { // Take clustergroup off line and read its status property when done TakeOffLine(clrg); clrg.Get(); Console.WriteLine(clrg["Status"]); System.Threading.Thread.Sleep(3000); // Sleep for a while // Bring back online and get status. BringOnLine(clrg); clrg.Get(); Console.WriteLine(clrg["Status"]); } } static void TakeOffLine(ManagementObject resourceGroup) { ManagementBaseObject outParams = resourceGroup.InvokeMethod("Takeoffline", null, null); } static void BringOnLine(ManagementObject resourceGroup) { ManagementBaseObject outParams = resourceGroup.InvokeMethod("Takeoffline", null, null); } } } 
      • what's the difference? - tCode
      • At TakeOffline & BringOnLine - Daniel Protopopov
      • Why do we need these methods, if even the case does not reach them? - tCode