Answer the question
In order to leave comments, you need to log in
Why PS incompletely fulfills the script?
$CurrentDate = Get-Date
$TargetFolder = "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\"
$DeadDate = $CurrentDate.AddDays(-5)
$NameDate = Get-Date -Format {dd-MM-yyyy-HHmm}
$SQLpathBackup = "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\Backup\"
$CredFSSRV = New-Object System.Management.Automation.PSCredential ("sys\1", $(ConvertTo-SecureString "1" -AsPlainText -Force))
new-psdrive -name A -psprovider FileSystem -root \\fssrv\1\databases -Credential $CredFSSRV
$DistFolder = "A:\"
function BackupDB {
param (
$DBNAME
)
Remove-Item -Path $($SQLpathBackup + $DBNAME + ".bak")
Backup-SqlDatabase -ServerInstance "127.0.0.1" -Database $DBNAME -CompressionOption On -BackupFile $($SQLpathBackup + $DBNAME + ".bak")
Copy-Item -Path $($TargetFolder + $DBNAME + ".bak") -Destination $($DistFolder + $DBNAME + "\" + $DBNAME + "_" + $NameDate + ".bak") -Force
GCI -Path $($DistFolder + $DBNAME) -Recurse | Where-Object {$_.CreationTime -LT $DeadDate} | RI -Recurse -Force
}
1BackupDB -DBNAME dev-data
2BackupDB -DBNAME app-hom
3BackupDB -DBNAME mdt-data
4BackupDB -DBNAME training
Answer the question
In order to leave comments, you need to log in
Write the error to a file and study
Maybe your system has no rights, for example. Or is the instance really needed in a different
Start-transcript format at the beginning
Backup-SqlDatabase -ServerInstance "127.0.0.1" -Database $DBNAME -CompressionOption On -BackupFile $($SQLpathBackup + $DBNAME + ".bak")Have you tried running this command manually? If I remember correctly, the ServerInstance parameter should look like "Computer\Instance"
$SQLpathBackup + $DBNAME + ".bak"this is the thing I would once calculate at the beginning of the function through Join-Path and substitute in the right places
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question