Creating a database master key
In this recipe, we will create a database master key.
Getting ready
We will create a database master key for the master database in this recipe. You can substitute a different database for this exercise if you wish.
The T-SQL equivalent of what we are trying to accomplish is:
USE master GO CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'P@ssword'
How to do it...
Let's list the steps required to complete the task:
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 as follows:#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:
$VerbosePreference = "Continue" $masterdb = $server.Databases["master"] if($masterdb.MasterKey -eq $null) { $masterkey...