Enumerating SQL Server instances on a remote Windows host
Programmatically enumerating a list of SQL Server instances from a remote Windows server can be challenging. If you have the Remote Registry activated, and know the name of the target server, you can fairly easily get the list of instance names from the server via the following VB code snippet.
1 2 3 4 5 6 7 8 9 10 11 |
Dim KeyType As Microsoft.Win32.RegistryHive = Microsoft.Win32.RegistryHive.LocalMachine Dim RemoteKey As Microsoft.Win32.RegistryKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(KeyType, "remote_machine_name" , Microsoft.Win32.RegistryView.Registry64) Dim SQLInstanceNamesKey As Microsoft.Win32.RegistryKey = RemoteKey.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL" , Microsoft.Win32.RegistryKeyPermissionCheck.ReadSubTree , System.Security.AccessControl.RegistryRights.QueryValues) For Each sName As String In SQLInstanceNamesKey.GetValueNames Console.WriteLine(sName) Next |
This code does nothing other than print the list of remote instances to the console, however it could form a simple wrapper for any number of processes you need to perform on newly discovered SQL Server instances.