Windows使用powershell计划任务创建问题

发布于 2024-11-15 16:26:19 字数 1254 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热鲨 2024-11-22 16:26:19

我会将 -command 选项换成 -file:

$taskToRun = "c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file ""$scriptFilePath""" 

此外,PowershellPack 有一个 TaskScheduler 模块,可以使任务调度变得更加容易:

http://archive.msdn.microsoft.com/PowerShellPack

[更新] 谢谢

完美,只需要用单引号而不是双引号转义

$taskToRun = "c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file '$scriptFilePath'";

I'd trade the -command option for -file:

$taskToRun = "c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file ""$scriptFilePath""" 

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

$taskToRun = "c:\windows\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file '$scriptFilePath'";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文