1

I have recently added a SQL Server JOB on SERVER-A that goes to SERVER-B and backups all the SSAS databases.

This seems to be fine.

However when I want to delete backups older than 3 days, using the function below, I get the error message on the picture below.

It says that the path is wrong, but that is exactly the path where my .abf files are.

How should I pass the path to powershell?

this is my function in powershell:

Function DeleteOldFiles
{
    try
    {
    $limit = (Get-Date).AddDays(-$DaysKeepBackup)
    Get-ChildItem -Path $checkFolder -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | foreach ($_) {Remove-Item $_.fullname -Force}
    #Write-Host "Backup files created before $limit have been deleted"
    }
    catch
    {
        Write-Error $_
    }
}

enter image description here

Now I have changed my function in Powershell.

Function DeleteOldFiles
{
    try
    {
    $limit = (Get-Date).AddDays(-$DaysKeepBackup)
    Get-ChildItem -Path  Microsoft.PowerShell.Core\FileSystem::$checkFolder -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | foreach ($_) {Remove-Item $_.fullname -Force}
    #Write-Host "Backup files created before $limit have been deleted"
    }
    catch
    {
        Write-Error $_
    }
}

Now I get a different error:

The job script encountered the following errors. These errors did not stop the script: A job step received an error at line 124 in a PowerShell script. The corresponding line is '
DeleteOldFiles '. Correct the script and reschedule the job. The error information returned by PowerShell is: 'Access is denied '

enter image description here

Marcello Miorelli
  • 16,170
  • 52
  • 163
  • 300
  • 1
    You might take a look at this question as well: http://dba.stackexchange.com/q/51870/507 –  Aug 11 '15 at 18:42
  • In your function at this point foreach ($_), remove the ($_). You can also add this at the top of the script and it is possible more information on the error will bubble up to the job history: $erroractionpreference = "Stop" –  Aug 12 '15 at 12:32

0 Answers0