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

Dropping a database snapshot


In this recipe, we will drop an existing database snapshot.

How to do it...

Here are the steps to drop a database snapshot:

  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
    
    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
  3. Add the following script and run it:

    $databaseSnapshotName = "SnapshotDB_SS"
    
    #source database
    $db = $server.Databases[$databaseName]
    
    #for our recipe, drop snapshot if exists
    if($server.Databases[$databaseSnapshotName])
    {
        $server.Databases[$databaseSnapshotName].Drop()
    }

How it works...

Dropping a database snapshot using PowerShell and SMO is very similar to dropping an actual physical database. You need to create a handle in your database snapshot and use the Drop method:

$server.Databases[$databaseSnapshotName].Drop()

See also

  • The Listing...