VBScript 使用 WMI 找出 SQL Server 版本

VBScript using WMI to find out SQL Server version(VBScript 使用 WMI 找出 SQL Server 版本)
本文介绍了VBScript 使用 WMI 找出 SQL Server 版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能指点我一个 vbscript(使用 WMI)来找出安装的 SQL Server 版本.我有一个场景,可以在一台机器上安装 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 搜索结果:

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 不是执行此操作的最快方法.您是否考虑过直接检查注册表而不是通过 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 的解决方案使用注册表,并假设可以安装 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

这篇关于VBScript 使用 WMI 找出 SQL Server 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)