我正在尝试使用powershell编写Windows计划任务创建脚本,其中计划任务调用包含空格的目录中的powershell脚本 . 所以我需要创建一个/ tr参数,如powershell.exe -noninteractive -command“&'c:\ temp \ program files \ a.ps1'”
这是我尝试过的一个例子
# Create the script file the schedule task will call, note path contains a space
'set-content c:\temp\program files\a.log "Just done @ $(get-date)"' > 'c:\temp\program files\a.ps1'
$scriptFilePath = 'c:\temp\program files\a.ps1';
$userName = read-host 'user name'; # will need log on as batch rights
$userPassword = read-host 'user password';
# The following looks good but schtasks args gets messed up so no creation
$taskToRun = "c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -command `"& '$scriptFilePath'`"";
$taskToRun
schtasks /create /tn 'ATest' /ru $userName /rp $userPassword /sc MINUTE /mo 1 /st '00:00' /f /tr $taskToRun;
# Gets imported but mangles the tr so instead of
$taskToRun = 'c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -command \"& ''' + $scriptFilePath + '''\"';
$taskToRun
schtasks /create /tn 'ATest' /ru $userName /rp $userPassword /sc MINUTE /mo 1 /st '00:00' /f /tr $taskToRun;
如果有人知道如何正确逃脱,我将不胜感激
提前致谢
拍
1 回答
我为-file交换了-command选项:
此外,PowershellPack还有一个TaskScheduler模块,可以更轻松地完成任务调度:
http://archive.msdn.microsoft.com/PowerShellPack
[更新]谢谢
完美,只需要用单引号而不是双引号来逃避