Shorts VBS Script to Determine if SQL Server is installed and running

I am playing with System Centre Operations Manager and Config Manager. They all use VBS to determine if SQL Server is present, but often fall foul of a stopped SQL Server service or a different major version (Sql 2008 v Sql 2005)and are very wordy – some 75 lines of code in the configuration Items for DCM!

For example the downloadable DCM packs for SQL Server only largely work on SQL 2005. Really we need them to work in any edition or at least 200x.

So here is a one line VBS script I have started using to detect if SQL Server Service is installed and running (any  version)

If GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select Name from Win32_Service where state='Running' and PathName like '%sqlservr.exe%'", "WQL", &H10).Count > 1 Then WScript.Echo “True”

Works great in System Centre Configuration Manager as a Detection Method for SQL Server. A sample screenshot in config manager is below.


Comments (3)

  1. Kerry - not a Microsoftie says:

    an interesting code snippet.

    it seems that there is a typo in your code: the 3rd parameter &H10 should not be in ExecQuery().

    Does it work for express editions of sql server? It does not seem to detect my sql server 2k5 express edition…

  2. Rob says:

    Wouldn’t your script also only detect running SQL instances with "… where state=’Running’ …" in the WQL Select statement?  I suspect omitting that portion, would yield any instance running or not.

    I’m preparing to implement the SQL MP for OpsMgr and will need to modify the discoveries to only detect "supported" instances of SQL, as our organization’s SQL support is fractured.  I, however, would like to discover all SQL instances to help make sure somone in our company is properly supporting each DBMS.

    Any updates you could provide to this posting would be appreciated.

  3. MSDN Archive says:


    Some updates based on the comments.

    The H10 flag is not compulsary  – it is an attempt at optimising the ExecQuery based on this article

    I haven’t a clue if this "optimisations" work in VBS, but I put them in anyway and it functionaly worked. i haven’t performance tested it to see if they make a difference.

    The code will work with SQL Express as the executable name is the same in the services list (sqlservr.exe)

    Yes, My script only detects running instances as in this project I was looking at config management of only running instances. You can take out the "running" to get installed instances.

    The SCOM SQL management pack by default will detect ALL instances of SQL – the detection rules are quite verbose, but it works for me.  If you do not want to support some instance of SQL – why not put them into a different group based on the version running ? If your not sure how to do this, post a comment and I’ll post soem details.

Skip to main content