Powershell和schtask与具有空间的任务

Powershell和schtask与具有空间的任务,powershell,windows-task-scheduler,Powershell,Windows Task Scheduler,我正在对powershell使用schtask命令。出现的问题是,当它是C:\Program Files\时,它认为路径只是C:\Program,而路径的其余部分是一个参数。我试图通过使用该字段的“pre”和“post”来逃避它,但确实有所不同。我如何才能做到这一点?我无法硬编码路径,因为用户安装它时可以更改路径 我在Windows 7 x64中创建了它。它创建了任务ok,脚本返回。但是,当我在任务计划中查看它时,任务的属性,然后是操作,然后点击编辑。它将程序显示为C:\program,然后将其

我正在对powershell使用schtask命令。出现的问题是,当它是C:\Program Files\时,它认为路径只是C:\Program,而路径的其余部分是一个参数。我试图通过使用该字段的“pre”和“post”来逃避它,但确实有所不同。我如何才能做到这一点?我无法硬编码路径,因为用户安装它时可以更改路径

我在Windows 7 x64中创建了它。它创建了任务ok,脚本返回。但是,当我在任务计划中查看它时,任务的属性,然后是操作,然后点击编辑。它将程序显示为C:\program,然后将其余部分显示为参数

脚本:

$folder = Split-Path $MyInvocation.MyCommand.Path -Parent
$app = "\Demon.DatabasePurge.exe"
$exe = $app.Insert(0, $folder)
schtasks /create /tn "Demon Purge Job" /sc daily /st 00:00:00 /tr $exe
以下是我尝试过的:

$folder = Split-Path $MyInvocation.MyCommand.Path -Parent
$app = "\Demon.DatabasePurge.exe`""
$exe = $app.Insert(0, $folder)
$exe2 = $exe.Insert(0, "`"")
schtasks /create /tn "Demon Purge Job" /sc daily /st 00:00:00 /tr $exe2

我在这方面也遇到了一些问题。其他人对此问题的答案是加入单引号

/TR "'C:\Program Files (x86)\etc\test.exe'"

我在Windows Server 2008 R2上也遇到了同样的问题。虽然表示您应该能够用\“我从来没有让它工作过,但单引号工作过。”。此外,带空格的参数也可以用单引号括起来:

schtasks /create /f /tn "Test" /tr "'c:\program files\test.bat' arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00
或者在构建要执行的字符串时(并使用$folder变量):

第一个示例生成以下任务操作屏幕:

若要解决此问题,请将任务的路径部分(不包括参数或开关)括在反斜杠
\
和引号
字符组合之间,例如
\”
。创建包含空格的路径或命令时,通常将任务的完整路径(包括参数或开关)括在引号之间

代码:

替换:

schtasks /create /f /tn "Test" /tr "\"c:\program files\test.bat\" arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00

我在C:\程序文件下使用notepad++exe进行了尝试,您的原始代码运行良好。它为我创建了一个计划任务。使用C:\Program Files\SyncToy 2.1\SyncToy.exe,您的原始代码也为我工作。你在哪个操作系统上工作?也许我不太清楚。我能够创建任务;但是,它不会运行。我用的是Win7。因此,我创建了任务,如果您浏览该任务,请右键单击“属性”、“操作”和“编辑”。我的任务会将程序显示为C:\program,然后将路径的其余部分显示为参数。我将更新描述。
schtasks /create /f /tn "Test" /tr "'c:\program files\test.bat' arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00
schtasks /create /f /tn "Test" /tr "\"c:\program files\test.bat\" arg1 'arg 2 with spaces' arg3" /sc Daily /st 00:00