問題描述
誰能指點(diǎn)我一個 vbscript(使用 WMI)來找出安裝的 SQL Server 版本.我有一個場景,可以在一臺機(jī)器上安裝 SQL Server 2008 R2 或 SQL Server 2012.
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 on a machine.
推薦答案
基于 此處的第一個 Google 搜索結(jié)果:
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
請注意,WMI 不是執(zhí)行此操作的最快方法.您是否考慮過直接檢查注冊表而不是通過 WMI?
Note that WMI is not the fastest way to do this. Have you considered checking the registry directly instead of going through WMI?
UPDATE 給出 OP 的解決方案使用注冊表,并假設(shè)可以安裝 2008R2 或 2012 中的一個:
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")
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
這篇關(guān)于VBScript 使用 WMI 找出 SQL Server 版本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!