我正在尝试使用Power Shell脚本创建数据库的备份,但是我需要根据自己选择的位置(即D:\
)创建该bak文件。
我正在使用SQL Server 2012,下面是我的脚本。
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum") | Out-Null
$dbToBackup = "Test_Dev"
$dbBackupFile = "D:\file.bak"
$server = New-Object Microsoft.SqlServer.Management.Smo.Server
$backup = New-Object Microsoft.SqlServer.Management.Smo.Backup
$backup.Action = "Database"
$backup.BackupSetName = "$dbToBackup Backup"
$backup.BackupSetDescription = "Full backup of $dbToBackup"
$backup.Database = $dbToBackup
$backup.Incremental = $false
$backupFile = New-Object Microsoft.SqlServer.Management.Smo.BackupDeviceItem $dbBackupFile, "Disk"
$backup.Devices.Add($backupFile) | Out-Null
$backup.SqlBackup($server)
但是脚本会引发如下错误:
新对象:无法为以下参数转换参数“ 1”,其值为:“ Disk” 输入“ BackupDeviceItem” “ Microsoft.SqlServer.Management.Smo.DeviceType”:“无法转换 值“磁盘”以键入“ Microsoft.SqlServer.Management.Smo.DeviceType” 由于枚举值无效。指定以下e之一 数值,然后重试。可能的枚举值为 “ LogicalDevice,磁带,文件,管道,VirtualDevice,Url”。”第26行: 字符:25 + $ backupFile =新对象<<<< Microsoft.SqlServer.Management.Smo.BackupDeviceItem $ dbBackupFile, “磁盘” + CategoryInfo:InvalidOperation:(:) [New-Object],MethodException + FullyQualifiedErrorId:ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
我无法跟踪问题,也无法理解该枚举是什么。