| title | Access the WMI Provider with VBScript | |||
|---|---|---|---|---|
| description | Learn how to create a VBScript program that lists the version of installed instances of SQL Server that are running on a computer. | |||
| author | markingmyname | |||
| ms.author | maghan | |||
| ms.date | 03/14/2017 | |||
| ms.service | sql | |||
| ms.subservice | wmi | |||
| ms.topic | reference | |||
| helpviewer_keywords |
|
|||
| dev_langs |
|
[!INCLUDE SQL Server] This section describes how to create a VBScript program that lists the version of installed instances of [!INCLUDEmsCoName] [!INCLUDEssNoVersion] that are running on a computer.
The code example lists the instances of [!INCLUDEssNoVersion] running on the computer and its version.
-
Open a new document in a text editor, such as [!INCLUDEmsCoName] Notepad. Copy the code that follows this procedure and save the file with a .vbs extension. This example is called test.vbs.
-
Connect to an instance of the WMI Provider for Computer Management with the VBScript
GetObjectfunction. This example connects to a remote computer named mpc, but omit the computer name to connect to the local computer: winmgmts:root\Microsoft\SqlServer\ComputerManagement. For more information about theGetObjectfunction, see the VBScript reference. -
Use the
InstancesOfmethod to enumerate a list of the services. The services can also be enumerated by using a simple WQL query and anExecQuerymethod instead of theInstancesOfmethod. -
Use the
ExecQuerymethod and a WQL query to retrieve the name and version of the installed instances of [!INCLUDEssNoVersion]. -
Save the file.
-
Run the script by typing cscript test.vbs at the command prompt.
set wmi = GetObject("WINMGMTS:\\.\root\Microsoft\SqlServer\ComputerManagement12")
for each prop in wmi.ExecQuery("select * from SqlServiceAdvancedProperty where SQLServiceType = 1 AND PropertyName = 'VERSION'")
WScript.Echo prop.ServiceName & " " & prop.PropertyName & ": " & prop.PropertyStrValue
next