V
V
Vladislav2021-07-02 16:13:51
PowerShell
Vladislav, 2021-07-02 16:13:51

Why PS incompletely fulfills the script?

spoiler

$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

The script is executed on behalf of the system, but ps does not complete it completely: only deleting old databases, but not creating new ones.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxKozlov, 2021-07-02
@skibenko_v

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

R
Roman Bezrukov, 2021-07-02
@NortheR73

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 question

Ask a Question

731 491 924 answers to any question