I've been trying to run the script below which is on my disk via a SQL Agent job with parameters but it just runs forever and never deletes any content.
# Set limit -x days
$limit = (Get-Date).AddDays(-$LimitDay)
# Path to Recurse
$path = "$TargetPath"
get-childitem -Path $path -Recurse -Force |? {$_.psiscontainer -and $_.lastwritetime -le $limit} |% {remove-item $_ -force -whatif}
In the SQL job I would enter;
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe c:\sysadmin\scripts\backuptrim.ps1 -TargetPath\\server1\foldername\Daily -LimitDay 14
I've also tried running the SQL job step as Operating System cmdshell and the PowerShell option. The SQL service accuont has full control access to the share and underlying folders too.
Any ideas? - thanks