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 backup device


This recipe will show how you can create a backup device using PowerShell.

Getting ready

We are going to create a backup device in this recipe. Here's the T-SQL equivalent of what we are trying to accomplish:

EXEC master.dbo.sp_addumpdevice @devtype = N'disk',
    @logicalname = N'Full Backups',
    @physicalname = N'C:\Backup\backupfile.bak'

How to do it...

Let's list the steps required to create a backup device:

  1. Open PowerShell ISE as administrator.

  2. Import the SQLPS module as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
  3. Add the following script and run:

    $instanceName = "localhost"
    $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
    
    #this file will be created by PowerShell/SMO
    $backupfilename = "Full Backups"
    $backupfile = "C:\Backup\backupfile.bak"
    
    #this line should be in a single line
    $backupdevice = New-Object Microsoft.SqlServer.Management.Smo.BackupDevice -ArgumentList $server, $backupfilename...