0

Any other time this works

C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe 
    -ExecutionPolicy RemoteSigned 
    -File "C:\Users\me\Documents\SQL Server Management Studio\Powershell Scripts\ps_A_PowerShell_Script.ps1 "

But if this needs a string argument with spaces, I can't get it to work. For example,

C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe 
    -ExecutionPolicy RemoteSigned 
    -File "C:\Users\me\Documents\SQL Server Management Studio\Powershell Scripts\ps_A_PowerShell_Script_With_Arguments.ps1 
    -Path 'F:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup' "

My PowerShell script is:

#---- declare parameters ----#
param([string]$Path=$(Throw "Parameter missing: -path Path"))

$DaysToKeep = "-7"
$CurrentDate = Get-Date
$DatetoDelete = $CurrentDate.AddDays($DaysToKeep)
Get-ChildItem $Path -Recurse | Where-Object { $_.LastWriteTime -lt $DatetoDelete } | Remove-Item -Recurse

And I'm running this from SQL Server Job Agent, using CmdExec.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
On The Net Again
  • 265
  • 4
  • 16
  • 4
    Looks like you have the quotes in the wrong place; try `C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -ExecutionPolicy RemoteSigned -File "C:\Users\me\Documents\SQL Server Management Studio\Powershell Scripts\ps_A_PowerShell_Script_With_Arguments.ps1" -Path "F:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup"` – TessellatingHeckler Jun 13 '23 at 18:16
  • That didn't seem to work – On The Net Again Jun 13 '23 at 18:33
  • 1
    What does "didn't work" mean? – Thom A Jun 13 '23 at 18:45
  • The job failed. Nothing got deleted. – On The Net Again Jun 13 '23 at 20:30
  • Make the script write the `$path` to a text file so you can see what it's receiving - and be sure it's running at all; my guess is they need quotes escaped somewhere, but I don't know why the powershell file doesn't need them and the path does. Maybe `C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -ExecutionPolicy RemoteSigned -File "C:\Users\me\Documents\SQL Server Management Studio\Powershell Scripts\ps_A_PowerShell_Script_With_Arguments.ps1" -Path "'F:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup'"` but that's another guess. – TessellatingHeckler Jun 13 '23 at 21:15
  • So it failed, but generated no errors..? – Thom A Jun 13 '23 at 21:51
  • Clearly, your quotes are incorrect in your submission. ```-File "C:\Users\me\Documents\SQL Server Management Studio\Powershell Scripts\ps_A_PowerShell_Script_With_Arguments.ps1 ``` has an unterminated double quote. Also ```-Path 'F:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup' "``` has an additional double quote and for some reason uses single quotes instead of double quotes. Please use the [Edit] facility so that we can see exactly what you are currently using. – Compo Jun 14 '23 at 10:52
  • @TessellatingHeckler please copy your initial answer as the actual answer. It worked. With a minor change. `C:\Windows\System32\WindowsPowerShell\v1.0\Powershell.exe -ExecutionPolicy RemoteSigned -File "C:\Users\clothere.grammont\Documents\SQL Server Management Studio\Powershell Scripts\ps_A_PowerShell_Script_With_Arguments.ps1" -BackupPath "F:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\MSSQL\Backup"` – On The Net Again Jun 21 '23 at 14:38

0 Answers0