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:
Change ANSI NULLS Enabled to False
Change ANSI PADDING Enabled to False
Restrict user access to RESTRICTED_USER
Set the database to Read Only
How to do it...
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
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 = "KERRIGAN" $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] #DatabaseOptions #change ANSI NULLS and ANSI PADDING $db.DatabaseOptions.AnsiNullsEnabled...