Skip to content Skip to sidebar Skip to footer

Vbscript Using Wmi To Find Out Sql Server Version

Can anyone point me to a vbscript (using WMI) to find out the installed SQL Server version. I have a scenario where either SQL Server 2008 R2 or SQL Server 2012 could be installed

Solution 1:

Based on the code in the first Google search result here:

Dim WMI, Col, Prod, Q
Set WMI = GetObject("WinMgmts:")
Q = "Select * FROM Win32_Product WHERE Vendor = " & _
    "'Microsoft Corporation' AND Name LIKE 'SQL Server%Database Engine Services'"Set Col = WMI.ExecQuery(Q)
ForEach Prod in Col
  if left(Prod.version, 3) = "11."then
    msgbox "SQL Server 2012 was found!" & vbCrLf & prod.version
  elseif left(Prod.version, 4) = "10.5"then
    msgbox "SQL Server 2008 R2 was found!" & vbCrLf & prod.version
  endifNextSet Col = NothingSet WMI = Nothing

Note that WMI is not the fastest way to do this. Have you considered checking the registry directly instead of going through WMI?

UPDATE given OP's solution using the registry instead, and with the assumption that exactly one of 2008R2 or 2012 could be installed:

RegKey2012 = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\" & _
             "Microsoft SQL Server\MSSQL11.MSSQLSERVER\"If RegKeyExists(RegKey2012) Then 
  WScript.StdOut.Write("2012") 
Else 
  WScript.StdOut.Write("2008R2") 
EndIfFunction RegKeyExists(Key) 
  Dim oShell, entry 
  OnErrorResumeNextSet oShell = CreateObject("WScript.Shell") 
  entry = oShell.RegRead(Key) 
  If Err.Number <> 0Then 
    Err.Clear 
    RegKeyExists = FalseElse 
    Err.Clear 
    RegKeyExists = TrueEndIfEndFunction

Post a Comment for "Vbscript Using Wmi To Find Out Sql Server Version"