O'Reilly logo

SQL Server 2014 with PowerShell v5 Cookbook by Donabel Santos

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating a condition

In this recipe, we will create a condition that will later be used for a policy, programmatically.

Getting ready

In this recipe, we will create a condition called xp_cmdshell is disabled that checks the Server Security facet, XPCmdShellEnabled.

How to do it...

These are the steps required to create a condition:

  1. Open PowerShell ISE as an administrator.
  2. Import the SQLPS module and create a new SQL Server object as follows:
    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    $connectionString = "server='localhost';Trusted_Connection=true"
    
    $conn = New-Object Microsoft.SQlServer.Management.Sdk.Sfc.SqlStoreConnection($connectionString)
    
    $policyStore = New-Object Microsoft.SqlServer.Management.DMF.PolicyStore($conn)
  3. Add the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required