Home > Articles > Data > SQL Server

SQL Server Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

SQL Server Policy-Based Management, Part Two

Last updated Mar 28, 2003.

Policy-Based Management, a new feature starting with SQL Server 2008, is a new way of managing your SQL Server systems. As I explained in Part One of this series, using a series of new settings, you can prescribe the way you want your server settings and database objects to be, and the system will take the responsibility to make things stay that way. In a sense, you’re defining your intent for the system, and the system maintains that intent.

You do all this using various Policies that you can define or import to your system. In the last tutorial I explained the parts of this new feature, and in this tutorial I’ll demonstrate a simple Policy that you can follow. In the final installment I’ll also show you the Policies that come “out of the box” with SQL Server 2008, where you can store them and run them from, and even how to run them with PowerShell against a server.

As you read through this tutorial, refer back to this primary graphic in Part One. This will help you track where you are in the process of creating a Policy.

Creating a Policy using Policy-Based Management

As I mentioned in the last tutorial, Policy-Based Management (PBM) is a SQL Server 2008 (and higher) feature only. You must create the Policy using SQL Server 2008, but you can run (which is actually called “Evaluating”) the Policy against SQL Server 2000 and 2005 servers, but you have to do that from the SQL Server 2008 system.

If you want to create your own Policy, open up SQL Server Management Studio (SSMS) on your SQL Server 2008 test system and follow along with me on the screens that I show here. I use a Virtual Machine for all my testing, so it stays self-contained and I can practice things like this without affecting any production systems.

Planning the Policy

Before you even get started, you need to plan what you want the Policy to do. You want to put that Policy in terms of the desired state — the way you want the Instance, database or database object to be. You’ll “evaluate” the server against the Policy, and it will tell you if the object is in that state. The one difference, as I mentioned in the last tutorial, is that in some cases you can actually prevent the object from taking that state. In this tutorial, I’ll stick with just reporting the violation of the desired state.

For this example, I’ve decided that none of my databases should be in the “Simple” recovery model. So the Policy statement I’ll work from is pretty simple:

“All databases must not be in the ‘Simple’ recovery model.”

I write them out, just like that. Notice that even though this is a negative condition (“No databases”), the test will be in the positive. What that means is that when the Policy is evaluated, it will only fail when it isn’t in the state I want. That limits what I have to go investigate — the only databases I really care about are the ones that are in “Simple” recovery model. All the others are OK. Keep that in mind as you design your own Policies, and at least for the first few, write them as simple sentences like this.

Creating the Policy

Once I open SSMS, I connect to a server, and then drill down to the Management | Policy Management | Policies node. Even if I want to store the Policy as a file and not in the database, it still has to be created here.

Next, I right-click that node and select “New Policy...” from the menu that appears.

<<Graphic: sqlsrv012309_PBM-2.tif>>

As I’ve mentioned before, whenever you see those three periods after a menu command, it means there are more choices to make. Making that menu selection brings up the first panel I need to fill out for the Policy:

At this stage all I do is name the Policy, and again, I keep that in to the state I want. I’ve got mine pretty specific here — I want to make sure that when if it evaluates to “False”, I know what that means.

Next I click on the “Description” link in the far left-hand part of this pane. That brings up an area where I can enter a more verbose description, which I’ll do. This description is presented to the user (or the developer to pass on to the user), so it needs to be very explanatory.

I can also set a “Category” for the Policy. This isn’t a requirement, but it does help me sort things later, so I add one called “Standards” for this particular example.

The “Text to display” box is presented to the user (or developer) in a separate area when the policy is violated. It’s a lot shorter, so I’ll enter a less verbose message here.

The “Address:” box contains text that also displays when the Policy is violated, but it is sent as a hyperlink. That way the user can click on the link to find out more information, or even open an e-mail to an address of my choice. I can use an HTTP, HTTPS, or MAILTO address as I have here.

With all of that filled out, I switch back to the “General” tab of this pane.

Assigning a Condition

Now it’s time to add the condition I’m checking for. Again, refer back to the graphic from the first tutorial if you need to check where you are. On the “General” tab of this panel, I click the arrow at the end of the “Check Condition” box and either select a condition I’ve made before, or “New condition...” to make a new one. Since I don’t have a condition for this already, I’ll make a new one now.

Once I click that line, I’m placed in a new panel to select a Facet (read that last tutorial for more on that) and the condition I’m looking for. First, I have to name it. I always use the specific item I’m testing here, since I might re-use this condition in another Policy someday. After that, I’ll select the “Database” facet, and then select the @RecoveryModel field. The comparison is “Not” (which is the != part) and the “Value” is “Simple”. It might look a little complicated, but if you read through the last tutorial again it makes sense. I click OK and I’m done.

Setting the Targets, Evaluation Mode and Server Restrictions

All that is left now is to set any targets I want. In this case, I want every database this way, but you might want to modify that to only user databases. If you want to change which databases this Policy will evaluate, then click the “Every” link you see here and you’ll enter the process for yet another condition. As you build your conditions you’ll find that you can use them over and over, which makes the process faster and easier.

I described the Evaluation Modes in the last tutorial, so I won’t cover that again here. I’ll set this one to “On demand”, which means I’ll run it manually whenever I want.

Finally, you can restrict the Policy to servers that meet your criteria using — once again — another condition. This condition, however, is limited to server facets.

I click OK, and the Policy is ready to run. At this point it is saved in the database of the server where I created it. Later I’ll explain how you can save it out to a file and run it from there.

Evaluating (or Running) a Policy

Just inside the “Policies” node, I right-click the name of the Policy I just created and then select “Evaluate” from the menu that appears. If you don’t see the screen the way I have it here, just press F7 on your keyboard.

Since I’m choosing to run the Policy from this node, it will run against this server. Once it runs, I see that I have some databases that fail the condition:

Clicking on the “View” link in the “Details” area at the bottom part of the panel shows me the parts of the condition that failed. That really isn’t necessary here since this is a simple condition — the Recover Model — but in a complex situation it matters which part of the condition fails.

In the next tutorial I’ll show you how to run the Policy from a file, against a remote server or servers, and how to run it from PowerShell.

InformIT Articles and Sample Chapters

There’s a lot more about PowerShell starting in this series of updates in this Reference Guide. You can read up on that before you move on to the next tutorial.

Books and eBooks

Ross Mistry has a great book on administration, Microsoft SQL Server 2005 Management and Administration, which will help you understand the general settings that you can use as Facet choices.

Online Resources

The official documentation for PBM is here.