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

Importing a policy


This recipe will show you how you can import a policy stored as an XML file into SQL Server.

Getting ready

In this recipe, we will use an XML policy that comes with the default SQL Server installation. This policy is called Database Page Verification.xml and is stored in C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Policies\DatabaseEngine\1033.

Feel free to substitute this with a policy that is available in your system.

How to do it...

These are the steps required to import a policy using PowerShell:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module:

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

    $connectionString = "server='localhost';Trusted_Connection=true"
    
    #set up connection
    $conn = New-Object Microsoft.SQlServer.Management.Sdk.Sfc.SqlStoreConnection($connectionString)
    
    #NOTE this is still called DMF, which stands for
    #PBM's old name, Declarative Management Framework
    $policyStore = New-Object Microsoft...