Various
ways to access registry
Program
1
If
RegKeyExists("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL
Server\MSSQL11.MSSQLSERVER\") Then
WScript.StdOut.Write("2012")
Else
WScript.StdOut.Write("2008R2")
End If
Function
RegKeyExists(Key)
Dim oShell, entry
On Error Resume
Next
Set oShell =
CreateObject("WScript.Shell")
entry =
oShell.RegRead(Key)
If Err.Number
<> 0 Then
Err.Clear
RegKeyExists = False
Else
Err.Clear
RegKeyExists = True
End If
End Function
Program 2 (using WMI)
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)
For Each 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
end if
Next
Set Col = Nothing
Set WMI = Nothing
Program 3 (using WMI)
' Supporting only SQL
Server 2012 and SQL Server 2008 R2
Const HKEY_LOCAL_MACHINE
= &H80000002
Set shell =
CreateObject("WScript.Shell")
strComputer =
"."
Set objRegistry =
GetObject("winmgmts:\\" & strComputer &
"\root\default:StdRegProv")
If Err.Number <> 0
Then
shell.LogEvent 4, "Get Object error"
shell.LogEvent 4, Err.Description
Else
End If
strKeyPath =
"SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL11.MSSQLSERVER\Setup"
strValueName =
"Version"
objRegistry.GetStringValue
HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
If Err.Number <> 0
Then
shell.LogEvent 4, "Get Stringvalue error"
shell.LogEvent 4, Err.Description
Else
End If
If IsNull(strValue) Then
WScript.StdOut.Write
("SQLNCLI10")
Else
WScript.StdOut.Write
("SQLNCLI11")
End If
Program 4 (using Reg.exe)
strkey="HKLM\SOFTWARE\Microsoft\Microsoft
SQL Server\MSSQL11.MSSQLSERVER\Setup" & Chr(34) & " /v
Version /reg:64"
Set objShell =
CreateObject("WScript.Shell")
Set objScriptExec =
objShell.Exec("REG QUERY " & Chr(34) & strkey )
strText =
objScriptExec.StdOut.ReadAll()
if (strText <>
"") then
WScript.echo
"2012"
else
WScript.echo
"2008R2"
end if
/reg:64
option is very important if you need to run the script via a 32 bit application
(example installer).
Use program 4 if you are using vbscript via some other medium (like installer).
No comments:
Post a Comment