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

Creating a certificate


This recipe shows how you can create a certificate using PowerShell and SMO.

Getting ready

In this recipe, we will create a certificate called Test Certificate that is protected by the database master key. You will need to make sure that the database master key has been created first for the database.

The T-SQL equivalent of what we are trying to accomplish in this recipe is as follows:

CREATE CERTIFICATE [Test Certificate]
WITH SUBJECT = N'This is a test certificate.',
START_DATE = N'04/10/2015',
EXPIRY_DATE = N'04/10/2017'

How to do it...

Let's take a look at the steps required to complete the task:

  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.Management.Smo.Server -ArgumentList $instanceName
  3. Add the following script and run...