VBScript: Enumerate All SQL Servers on a Domain

Filed under: Active Directory, Quick Code, VBScript — Written by Chrissy on Tuesday, January 9th, 2007 @ 11:14 pm

So if you have SQL Server installed locally, you're lucky enough to have access to SQLDeeMO. You can then easily enumerate SQL Servers with the following code. Note: If you do decide to use VBScript and SQLDeeMO, you will need to remove the "ee" in the script below. I didn't want to put in the actual object name so that people searching Google for sites that do not contain the phrase SQLDeeMO will still find my site.

SQLDeeMO VBScript Action

    Set objSQLDMOApp = CreateObject("SQLDeeMO.Application")
 
    Set objSQLList = objSQLDeeMOApp.ListAvailableSQLServers()
    For i = 1 To objSQLList.Count
       MsgBox objSQLList.Item(i)
    Next
 
    Set objSQLList = Nothing
    Set objSQLDeeMOApp = Nothing

But if you don't have SQL Server installed locally.. here's a hack that grabs the name of all Windows Servers in AD and then checks their registry for instances of SQL Server.

Enumerate All SQL Servers and Instance Names on a Domain

'****************************************************************************
' This script created by Chrissy LeMaire (clemaire@gmail.com)
' Website: http://netnerds.net/
'
' This script finds all SQL Servers and their instances that are members of an AD domain
' and running any Windows Server version
'
' Run this script with admin privs on any computer within a domain
'
' This script has only been tested on Windows Server 2003
'
' "What it does"
' 1. Gathers all machines in a domain that are running a Windows Server OS (NT, 2000, 2003)
' 2. Pings them to see if they are available
' 3. If they do respond to pings, it checks their registry to see if they have the proper SQL keys
' 4. If the key does exist, it then enumerates the instances (including default)
'
' NO WARRANTIES, USE THIS AT YOUR OWN RISK, etc.
'*****************************************************************************
Set objAdRootDSE = GetObject("LDAP://RootDSE")
Set objWMIService = GetObject("winmgmts:\.\root\cimv2")
Set objRS = CreateObject("adodb.recordset")
Const HKEY_LOCAL_MACHINE = &H80000002
 
  varConfigNC = objAdRootDSE.Get("defaultNamingContext")
  strConnstring = "Provider=ADsDSOObject"
  strWQL = "SELECT * FROM 'LDAP://" & varConfigNC & "' WHERE objectCategory= 'Computer' and OperatingSystem = 'Windows*Server*'"
  objRS.Open strWQL, strConnstring
      Do until objRS.eof
         Set objServer = GetObject(objRS.Fields.Item(0))
          strServerName = objServer.CN
 
          Set colItems = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address = '" & objServer.DNSHostName & "'")
          For Each objItem in colItems
              If objItem.StatusCode = 0 Then 'The Computer is Pingable
                    Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!" & strServerName & "\root\default:StdRegProv")
                      strKeyPath = "SOFTWARE\Microsoft\Microsoft SQL Server"
                      strValueName = "InstalledInstances"
                      objRegistry.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strValueName,arrValues
                        If IsNull(arrValues) = 0 Then 'It's a SQL Server! Enumerate it's instances
                          strMsg = strServerName & " is running the following SQL Instance(s): "
                          For Each strValue In arrValues
                              If strValue = "MSSQLSERVER" Then strValue = "Default Instance"
                                strMsg = strMsg & vbCrLf & strValue
                          Next
                          MsgBox strMsg
                        End If
                     Set objRegistry = Nothing
              End If
          Set objServer = Nothing
      Next
    objRS.movenext
    Loop
  objRS.close
 
Set objWMIService = Nothing
Set objRS = Nothing
Set objAdRootDSE = Nothing

If you are looking for the version of each SQL Server.. you can either query it with SELECT @@VERSION or it may be easily found in the registry. It's 10:47pm and I'm still at work so I'm heading out ;)

3 Comments   -
  • Comment by JM | February 20, 2007 @ 1:44 pm

    Very kewl!

    Cheers for posting it up!
    JM

  • Comment by Jerry | October 2, 2007 @ 7:34 pm

    Looks great. Where do I put the domain name?

  • Comment by Greg | May 15, 2008 @ 10:19 am

    Here's a slight modification - This script will query the SQL server for it's version. Also, creates an error file and an output file (this is all hack, nuthin' fancy
    '****************************************************************************
    ' This script created by Chrissy LeMaire (clemaire@gmail.com)
    ' Website: http://netnerds.net/
    '
    ' Modified by Gregory Jones (greg@fuzsh.com)
    '
    ' This script finds all SQL Servers and their instances and their version that are members of an AD domain
    ' and running any Windows Server version
    '
    ' Run this script with admin privs on any computer within a domain
    '
    ' This script has only been tested on Windows Server 2003
    '
    ' "What it does"
    ' 1. Gathers all machines in a domain that are running a Windows Server OS (NT, 2000, 2003)
    ' 2. Pings them to see if they are available
    ' 3. If they do respond to pings, it checks their registry to see if they have the proper SQL keys
    ' 4. If the key does exist, it then enumerates the instances (including default)
    '
    ' NO WARRANTIES, USE THIS AT YOUR OWN RISK, etc.
    '*****************************************************************************
    ========================================================================
    ON ERROR RESUME NEXT

    Set objAdRootDSE = GetObject("LDAP://RootDSE")
    Set objRS = CreateObject("adodb.recordset")
    set objFS = CreateObject("Scripting.FileSystemObject")
    Set objLogFile = objFS.CreateTextFile("sql_servers.txt")
    Set objERRORLog = objFS.CreateTextFile("sql_servers_errors.txt")
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")

    Const HKEY_LOCAL_MACHINE = &H80000002

    varConfigNC = objAdRootDSE.Get("defaultNamingContext")
    strConnstring = "Provider=ADsDSOObject"
    strWQL = "SELECT * FROM 'LDAP://" & varConfigNC & "' WHERE objectCategory= 'Computer' and OperatingSystem = 'Windows*Server*' "
    objRS.Open strWQL, strConnstring
    Do until objRS.eof
    Set objServer = GetObject(objRS.Fields.Item(0))
    strServerName = objServer.CN

    Set colItems = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address = '" & objServer.DNSHostName & "'")
    For Each objItem in colItems
    If objItem.StatusCode = 0 Then 'The Computer is Pingable

    Set objRegistry = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strServerName & "\root\default:StdRegProv")
    strKeyPath = "SOFTWARE\Microsoft\Microsoft SQL Server"
    strValueName = "InstalledInstances"
    objRegistry.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strValueName,arrValues
    If IsNull(arrValues) = 0 Then 'It's a SQL Server! Enumerate it's instances

    strMsg = strServerName & " is running the following SQL Instance(s): "
    For Each strValue In arrValues
    If strValue = "MSSQLSERVER" Then strValue = "Default Instance"
    strMsg = strServerName & vbtab & strValue & vbtab & GetSQLServerVersion(strServerName)
    Next

    objLogFile.WriteLine strMsg
    wscript.echo strMsg
    End If
    Set objRegistry = Nothing
    End If
    Set objServer = Nothing
    Next
    objRS.movenext
    Loop
    objRS.close

    Set objWMIService = Nothing
    Set objRS = Nothing
    Set objAdRootDSE = Nothing

    Function GetSQLServerVersion(serverName)

    Set cxn = Wscript.CreateObject("ADODB.Connection")
    strConn = "Driver={SQL Server};Server=" & strServerName & ";Database=master"
    sqlstr = "SELECT @@version"
    cxn.ConnectionString = strConn
    cxn.Open
    Set rs=cxn.Execute(sqlstr)

    rs.MoveFirst

    If ERR 0 Then

    objERRORLog.WriteLine Err.Description
    GetSQLServerVersion = Err.Description
    ERR.Clear
    Else
    GetSQLServerVersion = rs(0).Value

    End If

    cxn.Close

    End Function '//Function GetSQLServerVersion(serverName)

Leave your comment