Creating a backup device
This recipe shows how you can create a backup device using PowerShell.
Getting ready
We are going to create a backup device in this recipe. The equivalent T-SQL of what we are trying to accomplish is:
EXEC master.dbo.sp_addumpdevice @devtype = N'disk', @logicalname = N'Full Backups', @physicalname = N'C:\Backup\backupfile.bak'
How to do it...
Open the PowerShell console by going to Start | Accessories | Windows PowerShell | Windows PowerShell ISE.
Import the
SQLPS
module as follows:#import SQL Server module Import-Module SQLPS -DisableNameChecking
Add the following script and run:
$instanceName = "KERRIGAN" $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" $backupdevice = New-Object Microsoft.SqlServer.Management.Smo.BackupDevice($server,$backupfilename) #BackupDeviceType values are: #CDRom, Disk...