2013-11-15 29 views
0

我製作了備份我的遠程服務器的所有數據庫的腳本。當我從PowerShell ISE界面運行腳本時,我在備份設備中確定的路徑工作正常,但是當我從提示符運行它時,我設置的路徑被忽略,並且備份到SQL Server的默認備份目錄。爲什麼是這樣,我該如何解決它? 下面是我的腳本:從提示符運行PowerShell腳本時,SQL Server 2008不使用備份設備路徑

$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文件,並從提示符下執行,在設備的路徑將被忽略。 TKS

+0

任何最終的和完整的源代碼腳本工作? – Kiquenet

回答

1

我打賭,問題是這一行:

$backup.Devices.AddDevice($Dest + $db.Name + "_full_" + $timestamp + ".bak", "File"); 

您正在使用$目的地變量,這是不是在你的腳本中任何定義。我敢打賭,你在ISE會話中將$ Dest變量設置爲所需的路徑,這就是爲什麼它在那裏工作。

但是在新的控制檯窗口中,$ Dest爲空,所以SQL使用默認路徑。