1

I have a powershell script which deletes old backups from NAS storage. I have already tested it using Windows Powershell and it works but I couldn't perform the same thing in SQL Server. I have created a cmdExec type job and then wrote the script below (script is on desktop):

"C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe"

"C:\Users\domainAccount\Desktop\"

".\deleteold.ps1 -FolderPath \\192.168.x.x\Backups -FileAge 2 -ListOnly -LogFile \\192.168.x.x\Backups\log.log"

But I do not get any result as when I run the job it just keeps running infinitely until I stop that job.

1
  • Not sure how the 3 lines in your question are being used. A one-liner should work as long as the execution account has access to the referenced script and UNC paths: powershell -ExecutionPolicy RemoteSigned -File "C:\Users\domainAccount\Desktop\\deleteold.ps1" -FolderPath "\\192.168.x.x\Backups" -FileAge 2 -ListOnly -LogFile "\\192.168.x.x\Backups\log.log"
    – Dan Guzman
    Sep 8, 2019 at 14:27

1 Answer 1

0

You need to pass the powershell file in as an argument

powershell.exe -file C:\Users\domainAccount\Desktop\deleteold.ps1 -FolderPath \\192.168.x.x\Backups -FileAge 2 -ListOnly -LogFile \\192.168.x.x\Backups\log.log

Or if the step type doesn’t have to be CmdExe you can change the type to PowerShell

1
  • I would avoid using the built-in PowerShell step type. It doesn't use the latest version of PowerShell and I've found it to be far more trouble than it's worth. Just save it as a ps1 file and then invoke powershell.exe directly through a cmdexec step. Sep 9, 2019 at 12:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.