[^_^]PS[597]>Backup-Database -dbName:pubs -deviceNames:'d:\bak\pubs.bak' -deviceType:DontKnow
Backup-Database : Cannot convert value "DontKnow" to type "Microsoft.SqlServer.Management.Smo.DeviceType" due to invali
d enumeration values. The possible enumeration values are "LogicalDevice, Tape, File, Pipe, VirtualDevice".
At line:1 char:73
+ Backup-Database -dbName:pubs -deviceNames:'d:\bak\pubs.bak' -deviceType:D <<<<>
# author: Sung Kim
# Description: Create Full or Differential backups for
# SQL Server database with SQL Server Objects(SMO) using PowerShell
function Backup-Database {
param([string]$dbName = $(throw 'Enter Database name to backup'),
[string[]]$deviceNames = $(throw 'Enter device name(s)'),
[Microsoft.SqlServer.Management.Smo.DeviceType]$deviceType =
[Microsoft.SqlServer.Management.Smo.DeviceType]File,
[Microsoft.SqlServer.Management.Smo.Server]$server,
[switch]$Overwrite = $false,
[switch]$Differential = $false)
trap { break; }
# Load SMO assembly
$PRIVATE:SmoDir =
'C:\Program Files\Microsoft SQL Server\90\SDK\Assemblies'
[System.Reflection.Assembly]::LoadFile(
"$PRIVATE:SmoDir\Microsoft.SqlServer.Smo.dll") | out-null
# Initialize backup object
$PRIVATE:bkp = New-Object Microsoft.SqlServer.Management.Smo.Backup
$PRIVATE:bkp.Database = $dbName
$PRIVATE:bkp.Initialize = $Overwrite
$PRIVATE:bkp.Incremental = $Differential
# Add backup devices to backup database to
foreach ($PRIVATE:deviceName in $deviceNames) {
$PRIVATE:bkpItem =
new-object Microsoft.SqlServer.Management.Smo.BackupDeviceItem(
$PRIVATE:deviceName,
[Microsoft.SqlServer.Management.Smo.DeviceType]$deviceType)
$PRIVATE:bkp.Devices.Add($PRIVATE:bkpItem)
}
if ($Overwrite) {
Write-Warning "Overwriting existing backup set"
}
$PRIVATE:bkp.SqlBackup($server)
if ($Differential) {
Write-Host -NoNewLine -F Blue "Differential Backup: "
}
Write-Host -NoNewLine "$($dbName) has been backed up to "
Write-Host -ForegroundColor Cyan -NoNewLine "$($deviceNames)"
Write-Host " successfully"
}
Experimenting with a different format of blogs...