Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Listing policies


In this recipe, we will list policies deployed in our SQL Server instance.

Getting ready

Check which policies are being used in your environment using SQL Server Management Studio. Connect to SSMS and navigate to Management | Policy Based Management | Policies:

These are the same policies you should get after you run the PowerShell script in this recipe.

How to do it...

To list policies using PowerShell, perform the following steps:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
  3. Add the following script and run it:

    #replace with your server information
    $connectionString = "server='localhost';Trusted_Connection=true"
    
    $conn = New-Object Microsoft.SqlServer.Management.Sdk.Sfc.SqlStoreConnection($connectionString)
    
    #NOTE notice how the namespace is still called DMF
    #DMF - declarative management framework
    #DMF was the old reference to Policy Based Management
    $policyStore = New-Object Microsoft...