SQL Server 2008은 프롬프트에서 PowerShell 스크립트를 실행할 때 백업 장치 경로를 사용하지 않습니다.

StackOverflow https://stackoverflow.com//questions/20006835

문제

내 원격 서버의 모든 데이터베이스를 백업하는 스크립트를 작성했습니다.PowerShell ISE 인터페이스에서 스크립트를 실행하면 백업 장치에서 결정한 경로가 잘 작동하지만 프롬프트에서 실행할 때 IN 설정된 경로는 무시되고 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 파일을 저장하고 프롬프트에서 실행하면 장치의 경로가 무시됩니다.tks

도움이 되었습니까?

해결책

문제를 베팅하는 것은이 줄과 함께 있습니다.

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

스크립트의 어느 곳에서나 정의되지 않은 $ dest 변수를 사용하고 있습니다.ISE 세션에서 원하는 경로로 설정된 $ dest 변수가 있음을 알고 있습니다.

그러나 새 콘솔 창에서 $ dest는 null이므로 SQL이 기본 경로를 사용하고 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top