Reputation: 163
I want to schedule a Powershell script with an argument that has a space in it with schtasks.exe. (Powershell Version 2, Windows 7). Bearing in mind what I’ve read in this forum I’ve come up with this expression to be entered in the Powershell commandline:
schtasks.exe /create /f /tn "Mytaskname" /tr "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -command \`"& \`'D:\myscript.ps1\`' \`'D:\Filename with Space.xml\`'" /SD 08/04/2016 /ST 09:00 /RU domain\username /RP /SC ONCE
Unfortunately this doesn’nt work. If I schedule the task manually it works showing me the following entries in the task planner:
Programm / script
%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
Add arguments (optional)
-command "& 'D:\myscript.ps1' ''D:\Filename with Space.xml '"
Can anybody help? Thanks in advance
Upvotes: 2
Views: 8261
Reputation: 163
I overlooked that it wasn’t even enough to mimic the string for "Add arguments (optional)" that worked well when scheduling a powershell scirpt with space in arguments manually. I transformed JosefZ's cmd script into a powershell script.
$Taskname = "MyTaskName"
$Powershellpath = "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe"
$Powershellscript = "D:\PShell\SO\36493932 with spaces.ps1"
$Argument = "D:\bat\a b\testfile.txt"
$userdomain = “Domain”
$username = “User”
schtasks.exe /create /f /tn $Taskname /tr "$Powershellpath -command & \""\""\""$Powershellscript\""\""\"" \""\""\""$Argument\""\""\""" /SC ONCE /SD 10/04/2016 /ST 11:15 /RU $userdomain\$Username /RP
if($LASTEXITCODE -eq 0)
{
start-sleep -s 3
schtasks /Run /TN $Taskname
start-sleep -s 3
schtasks /End /TN $Taskname
}
Upvotes: 4
Reputation: 30103
Read about escaping rules:
&
ampersand character already escaped in double-quoted string)""
embedded double quotes)\"
embedded double quotes: learn by example in this description)Tested in following batch script. Note that ;pause
in -command
argument allows observing powershell script results for debugging purposes; auxiliary ST*
variables are used merely for better readability (STn
=name, STe
=executable, STc
=command, STp
=parameter):
@ECHO ON
@SETLOCAL EnableExtensions
set "STn=MyTaskName"
set "STe=%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe"
set "STc=D:\PShell\SO\36493932 with spaces.ps1"
set "STp=D:\bat\a b\testfile.txt"
schtasks.exe /create /f /tn "%STn%" /tr "%STe% -command & \"\"\"%STc%\"\"\" \"\"\"%STp%\"\"\";pause" /SC ONCE /SD 10/04/2016 /ST 11:00
@rem /RU %USERDOMAIN%\%username% /RP
@timeout /T 3 >NUL
schtasks /Run /TN "%STn%"
@pause
schtasks /End /TN "%STn%"
and following powershell
script (although this does not matter):
param([string] $InObject = $MyInvocation.InvocationName)
"{0} {1}" -f "listing:", "$InObject"
Get-Content "$InObject"
Output (note that powershell
script output is visible in another window):
==> D:\bat\SO\36472103.bat
==> set "STn=MyTaskName"
==> set "STe=C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe"
==> set "STc=D:\PShell\SO\36493932 with spaces.ps1"
==> set "STp=D:\bat\a b\testfile.txt"
==> schtasks.exe /create /f /tn "MyTaskName" /tr "C:\Windows\system32\WindowsPowerShell\v1.
0\powershell.exe -command & \"\"\"D:\PShell\SO\36493932 with spaces.ps1\"\"\" \"\"\"D:\bat\
a b\testfile.txt\"\"\";pause" /SC ONCE /SD 10/04/2016 /ST 11:00
SUCCESS: The scheduled task "MyTaskName" has successfully been created.
==> schtasks /Run /TN "MyTaskName"
SUCCESS: Attempted to run the scheduled task "MyTaskName".
Press any key to continue . . .
==> schtasks /End /TN "MyTaskName"
SUCCESS: The scheduled task "MyTaskName" has been terminated successfully.
==>
Upvotes: 1