Reputation: 2974
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. 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. Tks
Upvotes: 0
Views: 380
Reputation: 1666
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. 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.
But in a new console window, $Dest is null so SQL is using the default path.
Upvotes: 1