I have written a Windows service to collect information from all of our SQL servers. The service gets installed onto each server, and utilizing WMI and SMO, inserts relevant system information back to a central database. In order to get the SQL information, I use the following c# code:
List<Server> sqlServers = new List<Server>(); //List of Smo.Server objects
string registrySubKey = @"SOFTWARE\Microsoft\Microsoft SQL Server";
string registryValue = "InstalledInstances";
try
{
RegistryKey rk = Registry.LocalMachine.OpenSubKey(registrySubKey);
string[] instances = (string[])rk.GetValue(registryValue);
if (instances.Length > 0)
{
foreach (string element in instances)
{
Server s;
string serverInstance;
if (element == "MSSQLSERVER") //If default instance
{
serverInstance = System.Environment.MachineName;
}
else
{
serverInstance = System.Environment.MachineName + @"\" + element;
}
s = new Server(serverInstance);
if (s != null)
{
sqlServers.Add(s);
}
}
}
}
The only problem I'm having is on our SQL clusters. For those of you unfamiliar with active/passive sql clustering, you cannot connect directly to one of the nodes. Instead the sql cluster gets a virtual name and another ip address that clients would then connect to. The current code I have will try to connect to NodeName\instanceName which obviously will not work on the cluster. Instead I need to programmatically find the SQL cluster virtual name that this node belongs to and connect to that instead. I thought I might be able to get this information from the MSCluster_Cluster WMI class, but that will only get me the cluster virtual name, not the SQL cluster virtual name. Thanks in advance.