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

Enabling/disabling change tracking


This recipe shows you how you can enable and disable change tracking in your target database.

Getting ready

In this recipe, we will use a test database called TestDB. If you do not already have this database, log in to SQL Server Management Studio and execute the following T-SQL code:

IF DB_ID('TestDB') IS NULL
CREATE DATABASE TestDB
GO

You need to check which of your databases have change tracking enabled. To do this, connect to your instance using SQL Server Management Studio, and type in the following T-SQL statement:

SELECT
  DB_NAME(database_id) AS 'DB',
  *
FROM
  sys.change_tracking_databases

How to do it...

To enable/disable change tracking, perform the following steps:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module and create a new SMO Server object as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    #replace this with your instance name
    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer...