Using Policy Based Management for checking SQL Server Versions

By:   |   Updated: 2013-02-04   |   Comments (5)   |   Related: > Policy Based Management


Problem

Brady Upton explains in his tip how to create a SharePoint KPI that will display SQL Server versions. In this tip we will show how to check SQL Server versions on multiple instances using Policy Based Management.

Solution

To get a list of the different versions of SQL Server, we can use SqlSecurity.com to find SQL Server build numbers for the latest Service Packs. Then we can create a policy that compares current SQL Servers' versions with the latest Service Pack build. To evaluate the policy against all SQL Servers you can use Central Management Server or Registered Servers in SQL Server Management Studio (SSMS). In this tip we will evaluate the created policy against SQL Server 2005, SQL Server 2008 and SQL Server 2012.

For more info on Policy Based Management please review this tip: Using Policy Based Management in SQL Server 2008.

For this policy I am going to check the following versions and builds:

Create Conditions

We will create two conditions. One to use in the policy for server restriction (SQL Server 2005 or higher) and another one for actually checking the version and comparing it to the latest Service Pack build number.

Create the condition that will be used for the server restriction:

  • In SSMS go to the Management > Policy Management > Conditions
  • Right click "Conditions", then click "New Condition...":
    New Condition
  • Enter the name for the condition: "SQL Server 2005 or later"
  • Select "Server" facet
  • Select @VersionMajor field under "Expression" and add an expression to check if the version is higher than 9 (which is SQL Server 2005 or later):

    Condition 1

Create the condition that will be used in the policy to check the SQL Server version:

  • In SSMS go to the Management > Policy Management > Conditions
  • Right click "Conditions", then click "New Condition..."
  • Enter the name for the condition: "SQL Server version check"
  • Select "Server" facet
  • Select the @BuildNumber field under "Expression"
Condition 2
  • Add the following expression:
     
    Condition expression

    ExecuteSql('Numeric', 'SELECT CASE
    WHEN LEFT(CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20)), CHARINDEX(''.'', CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20))) - 1) = ''10'' THEN 4000
    WHEN LEFT(CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20)), CHARINDEX(''.'', CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20))) - 1) = ''9'' THEN 5000
    WHEN LEFT(CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20)), CHARINDEX(''.'', CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20))) - 1) = ''11'' THEN 3000
    END')

  • Click "OK"
    Note: The expression currently checks if the following Service Packs installed: SQL Server 2005 SP 4, SQL Server 2008 R2 SP2, SQL Server 2012 SP1. This condition will have to be updated with the latest service pack build when the new Service Pack is released.
  • Click "OK" to save the condition.

Create Policy

  • In SSMS go to the Management > Policy Management > Policies:
  • Right click "Policies", then click "New Policy...":
    New Policy
  • Enter the name for the policy: "SQL Server Service Pack"
  • Select condition "SQL Server Version check" under "Check Condition"
  • Select condition "SQL Server 2005 or later" under "Server Restriction"

    Policy

  • Click "OK" to save the policy.

Evaluate the policy

You can use the Registered Servers in SSMS to evaluate the policy. Refer to this tip for more information.

You can also evaluate the policy using the Central Management Server. Refer to my previous tip for the details.

Review the results after you evaluated the policy:

Review results

Click on the "View..." link for the failed server under "Details" column:

Details
Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Svetlana Golovko Svetlana Golovko is a DBA with 13 years of the IT experience (including SQL Server and Oracle) with main focus on performance.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2013-02-04

Comments For This Article




Saturday, April 23, 2016 - 4:24:06 PM - Svetlana Back To Top (41318)

Hi Dave,

 

I have never seen this before. I can thin only of the connection timeout related to the registered servers. The default timeout is 15 seconds when you register your SQL Servers in CMS. Try to increase it.

 

Svetlana


Tuesday, April 19, 2016 - 2:37:55 AM - Dave Back To Top (41263)

Gday, Excellent article. I have attempted this but have found one problem when evaluating using a CMS. When the Policy hits a server it cant connect to it times out and doesnt move on to the next server. Any ideas?

Cheers 

 


Tuesday, March 19, 2013 - 6:04:25 PM - Sumon Basu Back To Top (22882)

Svetlana,

Thanks for your post. I tried to implement the policy in my environment, where maximum of the instances are named. In some of the servers I am getting an error "cannot configure property..... [ExecuteSql statement]..... because the result is not deterministic". I tried to fix the issue, but was unable to do the same. Can you please help.

Thanks & Regards,

Sumon


Tuesday, February 5, 2013 - 10:45:15 AM - Svetlana Golovko Back To Top (21928)

Hi David,

This is a good point. Thank you!

It's pretty easy to add another version. To add SQL Server 2008 SP3 to the check you can change the expression to this:

ExecuteSql('Numeric', 'SELECT CASE
WHEN LEFT(CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20)), CHARINDEX(''.'', CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20))) + 2) = ''9.00'' THEN 5000
WHEN LEFT(CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20)), CHARINDEX(''.'', CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20))) + 2) = ''10.00'' THEN 5500
WHEN LEFT(CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20)), CHARINDEX(''.'', CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20))) + 2) = ''10.50'' THEN 4000
WHEN LEFT(CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20)), CHARINDEX(''.'', CAST(SERVERPROPERTY (''ProductVersion'') as VARCHAR(20))) + 2) = ''11.0.'' THEN 3000
END')


Monday, February 4, 2013 - 6:03:13 PM - David Back To Top (21909)

Svetlana,

 

I enjoyed the article and worked through setting it up. What would be nice to see is being able to evaluate SQL 2008 and SQL 2008 R2 by themseleves as they are at different levels. Can you check the level using '10.00' and '10.50'. That would ge great.

 

David















get free sql tips
agree to terms