How do I pass a string argument with spaces to a powershell script called by cmdexec in SQL Server Agent Job?

475 Views Asked by At

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.

0

There are 0 best solutions below