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:
Open PowerShell ISE as administrator.
Import the
SQLPS
module as follows:#import SQL Server module Import-Module SQLPS -DisableNameChecking
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...