Enumerate SQL Server Names from “SQL Server Enterprise Manager”

Occasionally, SQL
Server database administrators are requested to provide a list of development
SQL Servers, production SQL Servers and so on. Many articles, such as "Monitor
Disk Space on Multiple SQL Servers
"
and "Inventorying
hardware and OS information on all SQL Servers
"
require a list of
servers in a text file.

Usually
Database administrators do not have the list in hand or have never made a list,
but it is always available on the Enterprise manager since most of the servers
are registered in Enterprise Manager.

Servers
that are registered in Enterprise manager are stored in the registry of the
client machine. This article explains how to take advantage of Windows
Scripting Host and VBScript to read the registry in order to enumerate all of
the SQL servers that are registered in Enterprise Manager.

Pre-requisite

a. 
SQL Server 2000 Client is
installed on the machine where we are executing the script and most of the
servers are already registered in Enterprise manager.

b. 
VBScript and
WSH need the ability to be executed on the client machine.

Step 1

Create the folder C:\Scan
[Refer Fig 1.0]



Fig 1.0

Step 2

Download the VB Script
below and save it as C:\Scan\scanEM.vbs [Refer Fig 1.0]


‘Save this file to c:\scan\scanEM.vbs
‘Execute it on the command propmt
‘Created by : MAK
‘Date: March 18, 2005

Set objArgs = WScript.Arguments

GroupName= objArgs(0)

mySkey =
“SOFTWARE\Microsoft\Microsoft SQL Server\80\Tools\SQLEW\Registered Servers X” & “\” & GroupName

Const HKCR=&H80000000 ‘HKEY_CLASSES_ROOT
Const HKCU=&H80000001 ‘HKEY_CURRENT_USER
Const HKLM=&H80000002 ‘HKEY_LOCAL_MACHINE
Const HKU=&H80000003 ‘HKEY_USERS
Const HKCC=&H80000005 ‘HKEY_CURRENT_CONFIG

Const REG_SZ=1
Const REG_EXPAND_SZ=2
Const REG_BINARY=3
Const REG_DWORD=4
Const REG_MULTI_SZ=7

Const KEY_QUERY_VALUE=&H1
Const KEY_SET_VALUE=&H2
Const KEY_CREATE_SUB_KEY=&H4
Const KEY_ENUMERATE_SUB_KEYS=&H8
Const KEY_NOTIFY=&H10
Const KEY_CREATE_LINK=&H20
Const DELETE=&H10000
Const READ_CONTROL=&H20000
Const WRITE_DAC=&H40000
Const WRITE_OWNER=&H80000

Set WshShell = WScript.CreateObject(“WScript.Shell”)
Set oReg=GetObject(“winmgmts:!root/default:StdRegProv”)

‘wscript.echo EnumKey
(HKCU, “SOFTWARE\Microsoft\Microsoft SQL Server\80\Tools\SQLEW\Registered Servers X”)
‘wscript.echo “——————————–”
wscript.echo EnumValues(HKCU,myskey)

‘*******************
‘* Function Name: EnumKey
‘* Inputs: Key, Subkey
‘* Example: wscript.echo EnumKey(HKCU, “Software\Microsoft”)
‘* Returns: List of all sub keys
‘*******************

Function EnumKey(Key, SubKey)
Dim Ret()
oReg.EnumKey Key,SubKey, sKeys
ReDim Ret(UBound(sKeys))

For Count = 0 to UBound(sKeys)
Ret(Count) = sKeys(Count)
Next

EnumKey = Join(Ret,vbCrLf)
End Function

‘*******************
‘* Function Name: EnumValues
‘* Inputs: Key, Subkey
‘* Example wscript.echo EnumValues(HKCU, “Software\Microsoft”)
‘* Returns: List of all values in the format;
‘* NAME,TYPE,VALUE
‘*******************

Function EnumValues(Key, SubKey)
Dim Ret()
oReg.EnumValues Key,SubKey, sKeys, iKeyType ‘fill the array
ReDim Ret(UBound(sKeys))

For Count = 0 to UBound(sKeys)
Select Case iKeyType(Count)
Case REG_SZ
oReg.GetStringValue Key,SubKey, sKeys(Count), sValue
Ret(Count) = sKeys(Count) & “,” & “REG_SZ” & “,” & sValue
Case REG_EXPAND_SZ
oReg.GetExpandedStringValue Key,SubKey, sKeys(Count), sValue
Ret(Count) = sKeys(Count) & “,” & “REG_EXPAND_SZ” & “,” & sValue
Case REG_BINARY
oReg.GetBinaryValue Key,SubKey, sKeys(Count), aValue
Ret(Count) = sKeys(Count) ‘& “,” & “REG_BINARY” & “,” & Join(aValue,””)
Case REG_DWORD
oReg.GetDWORDValue Key,SubKey, sKeys(Count), lValue
Ret(Count) = sKeys(Count) & “,” & “REG_DWORD” & “,” & lValue
Case REG_MULTI_SZ
oReg.GetMultiStringValue Key,SubKey, sKeys(Count), sValue
Ret(Count) = sKeys(Count) & “,” & “REG_MULTI_SZ” & “,” & Join(sValue,””)
End Select
Next
EnumValues = Join(Ret,vbCrLf)
End Function

Download scanEM.vbs_.

Latest Articles