从提示符运行 powershell 脚本时,SQL Server 2008 不使用备份设备路径
Posted
技术标签:
【中文标题】从提示符运行 powershell 脚本时,SQL Server 2008 不使用备份设备路径【英文标题】:SQL Server 2008 doesn't use backup device path when running powershell script from prompt 【发布时间】:2013-11-15 17:16:25 【问题描述】:我制作了一个脚本来备份我的远程服务器的所有数据库。当我从 PowerShell ISE 界面运行脚本时,我在备份设备中确定的路径工作正常,但是当我从提示符运行它时,我设置的路径被忽略,并且备份到 SQL Server 的默认备份目录.为什么会这样,我该如何解决? 以下是我的脚本:
$DestSqlTpb = "E:\BackupBD\SQL-TPB-01\";
$PastaRede = "\\sql-tpb1-01\BackupBD\SQL-TPB-01\";
Write-Output ("Started DELETE at: " + (Get-Date -format yyyy-MM-dd-HH:mm:ss));
$ComandoDeleteRede = $PastaRede + "*"
remove-item $ComandoDeleteRede
$ComandoSqlTpbLocal = $DestSqlTpb + "*"
remove-item $ComandoSqlTpbLocal
Write-Output ("Finished DELETE at: " + (Get-Date -format yyyy-MM-dd-HH:mm:ss));
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.ConnectionInfo');
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.Management.Sdk.Sfc');
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO');
[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMOExtended');
$Server = "SQL-TPB1-01\SQL2008"; # SQL Server Instance.
$srv = new-object ("Microsoft.SqlServer.Management.Smo.Server") $Server
Write-Output ("Started SQL-TPB1-01 at: " + (Get-Date -format yyyy-MM-dd-HH:mm:ss));
$dbs = $srv.Databases
foreach ($db in $dbs)
if (($db.Name -ne "tempdb") -and (!$db.IsDatabaseSnapshot)) #We don't want to backup the tempdb database
$timestamp = Get-Date -format yyyy-MM-dd-HH-mm-ss;
$backup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup");
$backup.Action = "Database";
$backup.Database = $db.Name;
$backup.Devices.AddDevice($Dest + $db.Name + "_full_" + $timestamp + ".bak", "File");
$backup.BackupSetDescription = "Full backup of " + $db.Name + " " + $timestamp;
$backup.Incremental = 0;
$backup.CompressionOption = 1;
# Starting full backup process.
$backup.SqlBackup($srv);
# For db with recovery mode <> simple: Log backup.
If ($db.RecoveryModel -ne 3)
$timestamp = Get-Date -format yyyy-MM-dd-HH-mm-ss;
$backup = New-Object ("Microsoft.SqlServer.Management.Smo.Backup");
$backup.Action = "Log";
$backup.Database = $db.Name;
$backup.Devices.AddDevice($Dest + $db.Name + "_log_" + $timestamp + ".trn", "File");
$backup.BackupSetDescription = "Log backup of " + $db.Name + " " + $timestamp;
#Specify that the log must be truncated after the backup is complete.
$backup.LogTruncation = "Truncate";
# Starting log backup process
$backup.SqlBackup($srv);
;
;
;
Write-Output ("Finished SQL-TPB1-01 at: " + (Get-Date -format yyyy-MM-dd-HH:mm:ss));
Write-Output ("Started COPY at: " + (Get-Date -format yyyy-MM-dd-HH:mm:ss));
copy-item $ComandoDeleteRede $DestSqlTpb
Write-Output ("Finished COPY at: " + (Get-Date -format yyyy-MM-dd-HH:mm:ss));
当我只是从 ISE 运行时,它运行良好,但如果我保存 ps1 文件并从提示符执行,设备中的路径将被忽略。提示
【问题讨论】:
任何最终和完整的源代码脚本都可以工作吗? 【参考方案1】:我打赌问题出在这条线上:
$backup.Devices.AddDevice($Dest + $db.Name + "_full_" + $timestamp + ".bak", "File");
您正在使用 $Dest 变量,该变量未在脚本中的任何位置定义。我敢打赌,您在 ISE 会话中将 $Dest 变量设置为您想要的路径,这就是它在那里工作的原因。
但在新的控制台窗口中,$Dest 为空,因此 SQL 使用默认路径。
【讨论】:
以上是关于从提示符运行 powershell 脚本时,SQL Server 2008 不使用备份设备路径的主要内容,如果未能解决你的问题,请参考以下文章
在带有任务计划程序信息的提示符下运行 powershell 时出错 - “不被识别为 cmdlet、函数、脚本文件或可操作的名称..”