English 中文(简体)
Programmatically detect Windows cluster configuration?
原标题:

Does anyone know how to programatically detect that a Windows server is part of a cluster?

Further, is it possible to detect that the server is the active or passive node?

[Edit] And detect it from Win32? A registry setting maybe?

Thanks for any insight.

Doug

最佳回答

I don t have an exact answer, but there are lots of APIs beginning with "Cluster" (like ClusterOpenEnum and ClusterNodeEnum) and COM interfaces beginning with "IGetCluster" that look promising.

问题回答

You can use WMI to lookup the information. That should work from XP/Win32 etc.

There is some great information here on using VBScript to do the job: http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/clustering/

Here s some C#/.Net code that also uses WMI:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Management;

namespace SandboxConsole
{
    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 domainuser 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);
        }
    }
}

I found this code here and tidied it up a little.

Any specific language you re looking for?

You might be able to employ the failover cluster cmdlets for Powershell (for Windows Server 2008 R2). Specifically Get-Cluster and Get-ClusterNode





相关问题
Why running a service as Local System is bad on windows?

I am trying to find out the difference between difference service account types. I tumbled upon this question. The answer was because it has powerful access to local resources, and Network Service ...

Programmatically detect Windows cluster configuration?

Does anyone know how to programatically detect that a Windows server is part of a cluster? Further, is it possible to detect that the server is the active or passive node? [Edit] And detect it from ...

get file icon for Outlook appointment (.msg)

I ve read Get File Icon used by Shell and the other similar posts - and already use SHFileInfo to get the associated icon for any given extension, and that works great. However, Outlook uses ".msg" ...

Identifying idle state on a windows machine

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn t mean the ...

Terminating a thread gracefully not using TerminateThread()

My application creates a thread and that runs in the background all the time. I can only terminate the thread manually, not from within the thread callback function. At the moment I am using ...

热门标签