Windows使用powershell计划任务创建问题
我正在尝试使用 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;
如果有人知道如何逃脱正确,我希望得到提示,
提前
谢谢帕特
I'm trying to script windows scheduled task creation with powershell, where the schedules tasks call powershell scripts that are in a directory that contains a space. So i need to create with a /tr argument like powershell.exe -noninteractive -command "& 'c:\temp\program files\a.ps1'"
Here is a sample of what i have tried
# 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;
If anyone knows how to escape correctly, i would appreciate a hint
Thanks in advance
Pat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将 -command 选项换成 -file:
此外,PowershellPack 有一个 TaskScheduler 模块,可以使任务调度变得更加容易:
http://archive.msdn.microsoft.com/PowerShellPack
[更新] 谢谢
完美,只需要用单引号而不是双引号转义
I'd trade the -command option for -file:
Also, the PowershellPack has a TaskScheduler module that's makes task scheduling much easier:
http://archive.msdn.microsoft.com/PowerShellPack
[UPDATE] Thanks
Perfect, just needed to escape with a single quote rather than double quote