Book Image

PowerShell for SQL Server Essentials

By : Donabel Santos
Book Image

PowerShell for SQL Server Essentials

By: Donabel Santos

Overview of this book

Table of Contents (15 chapters)
PowerShell for SQL Server Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Implementing Reusability with Functions and Modules
Index

Scripting database objects


Being able to script database objects is a powerful capability. There may be times when you'll need to have scripts handy, whether for versioning purposes or simply to provide them to your developers.

SMO largely drives this scripting capability. There is a class called Microsoft.SqlServer.Management.Smo.Scripter, which can be passed a collection of SMO objects to script:

$script.Script($smoObjects)

For example, if you need to script all stored procedures, you can add all the stored procedures into an array and pass this array to the Scripter object. You need to ensure that you are passing the actual stored procedure object and not just the names.

The Scripter object also accepts ScriptingOptions. There are a number of options that can be set. They include the following options:

  • DriAll

  • DriIndexes

  • DriNonClustered

  • DriPrimaryKey

  • DriUniqueKeys

  • Encoding

  • SchemaQualify

  • ScriptDrops

  • ScriptOwner

Note

The complete documentation for all properties that can be set using the ScriptingOptions...