This is the second part in a series about programming policy-based management. The series starts here.
To build our MailOffByDefault policy we need:
Condition that specifies properties and settings
Policy that uses the condition
Condition first. This looks pretty straighforward.
Condition con1 = new Condition(ps, "MailOffSMO");
con1.Facet = "ISurfaceAreaFacet";
con1.ExpressionNode = ExpressionNode.Parse(
"@DatabaseMailEnabled = 0 and @SqlMailEnabled = 0");
The Condition class uses PolicyStore instance (ps) and names the condition (MailOffSMO). You initialize a facet with a string. So where did the string come from? It is named in the SMO library but I "found" it by making an equivalent condition with SSMS and inspecting it.
foreach (Condition c in ps.Conditions)
// … look at c in the VS visualizer
ExpressionNode should "define" my condition, and since there is no ExpressionNode constructor, I first tried Parse(). This created the Condition, but it was unusable in SSMS. SSMS wanted "false" not "0". I searched around for how to specify "false" in the parse string for a while, then came upon something better.
ExpressionNode has six subclasses that can be used in combination to specify any set of expressions that you need. These are
ExpressionNodeChildren (with subclasses)
ExpressionNodeFunction, ExpressionNodeGroup, and ExpressionNodeOperation