Altering database properties
This recipe shows you how to change database properties using SMO and PowerShell.
Getting ready
Create a database called TestDB
by following the steps in the Creating a database recipe.
Using TestDB
, we will perform the following tasks:
Change
ANSI NULLS Enabled
toFalse
Change
ANSI PADDING Enabled
toFalse
Change compatibility version to
110
(SQL Server 2012)Restrict user access to
RESTRICTED_USER
Set the database to
Read Only
How to do it...
Let's look at the steps involved in altering databases using PowerShell:
Open PowerShell ISE as administrator.
Import the SQLPS module and create a new SMO Server Object:
#import SQL Server module Import-Module SQLPS -DisableNameChecking #replace this with your instance name $instanceName = "localhost" $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
Add the following script and run:
#database $dbName = "TestDB" #we are going to assume db exists $db = $server.Databases[$dbName...