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 $_
}
}
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 '


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"