简体   繁体   English

从提示符运行Powershell脚本时,SQL Server 2008不使用备份设备路径

[英]SQL Server 2008 doesn't use backup device path when running powershell script from prompt

I made a script that backups all of the databases of my remote server. 我制作了一个脚本,用于备份远程服务器的所有数据库。 When I run the script from the PowerShell ISE interface, the path that I determine in the backup device works fine, but when I run it from the prompt, the path I set is ignored, and the backup is made to SQL Server's default backup directory. 当我从PowerShell ISE界面运行脚本时,我在备份设备中确定的路径可以正常工作,但是当我从提示符下运行脚本时,我设置的路径将被忽略,并且将备份到SQL Server的默认备份目录中。 Why is that, and how can I fix it? 为什么会这样,我该如何解决? Below is my script: 下面是我的脚本:

$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));    

When I just Run from ISE, it goes well, but if I save the ps1 file, and execute from prompt, the path in the device is ignored. 当我仅从ISE运行时,它运行良好,但是如果我保存ps1文件并从提示符处执行,则设备中的路径将被忽略。 Tks ks

I'm betting the problem is with this line: 我敢打赌问题出在这一行:

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

You are using the $Dest variable, which is not defined anywhere in your script. 您正在使用$ Dest变量,该变量未在脚本中的任何位置定义。 I bet that you have the $Dest variable set to the path you want in the ISE session, which is why it's working there. 我敢打赌,您已将$ Dest变量设置为ISE会话中所需的路径,这就是它在此处工作的原因。

But in a new console window, $Dest is null so SQL is using the default path. 但是在新的控制台窗口中,$ Dest为空,因此SQL使用默认路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM