Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
Database Tools
SQL Scripts & Samples
Links
» Database Forum
» Sitemap
Free Newsletters:
DatabaseDaily  
News Via RSS Feed


follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

MS SQL

Posted Jun 1, 2005

Enumerate SQL Server Names from "SQL Server Enterprise Manager"

By Muthusamy Anantha Kumar aka The MAK

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_.



MS SQL Archives

Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 




Latest Forum Threads
MS SQL Forum
Topic By Replies Updated
SQL 2005: SSIS: Error using SQL Server credentials poverty 3 August 17th, 07:43 AM
Need help changing table contents nkawtg 1 August 17th, 03:02 AM
SQL Server Memory confifuration bhosalenarayan 2 August 14th, 05:33 AM
SQL Server Primary Key and a Unique Key katty.jonh 2 July 25th, 10:36 AM


















Thanks for your registration, follow us on our social networks to keep up-to-date