windows - 从 powershell 触发任务调度程序作业

标签 windows powershell scheduled-tasks taskscheduler

我想创建一个作业,在周一到周五的早上 6 点到晚上 9 点之间运行,并以 15 分钟的间隔触发,如果运行时间超过 10 分钟,该作业应该终止。

我试过下面的代码:

$action = New-ScheduledTaskAction -Execute Powershell.exe
$trigger = New-ScheduledTaskTrigger -Weekly -At 6:30AM -DaysOfWeek 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday' 
$task = Register-ScheduledTask -TaskName "TaskName" -Trigger $trigger -Action $action -RunLevel Highest 
$task.Triggers.ExecutionTimeLimit = 'PT30M'
$task.Triggers.Repetition.Duration = 'PT15H' 
$task.Triggers.Repetition.Interval= 'PT15M'
$task.Triggers.Repetition.Duration = 'PT15H' 
$task | Set-ScheduledTask -User "UserName" -Password "Password"

我已经实现了所有其他目标,除了如果运行时间超过 10 分钟则终止作业。 我遇到了以下错误。

The property 'ExecutionTimeLimit' cannot be found on this object. Verify that the property exists and can be set.
At line:4 char:1
+ $task.Triggers.ExecutionTimeLimit = 'PT10M'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

请帮助我解决这个问题。提前致谢。

最佳答案

我认为任务设置执行限制是您正在寻找的:

$task.Settings.ExecutionTimeLimit =  'PT30M'

相同的命令行版本:

 $settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Minutes 30)

 Set-ScheduledTask -TaskName 'taskname' -Settings $settings

关于特性的引用:https://technet.microsoft.com/en-us/library/cc722178(v=ws.11).aspx

有关触发器执行限制和任务设置执行限制的有用讨论:https://superuser.com/questions/506662/what-is-the-difference-between-stop-the-task-if-it-runs-longer-than-inside-tri

关于windows - 从 powershell 触发任务调度程序作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491658/

相关文章:

javascript - NodeJS - 在 Windows 上解析另一个 JSON 中的 JSON 字符串时出现问题

使用 FindFirstFile/FindNextFile 删除文件夹中所有文件的 C++ 代码

PowerShell:新时间跨度格式结果

java - 在 Quartz 中使用 JobStoreCMT - 防止自动提交

Java 作业调度 : Is this possible with Quartz, 如果不是,我的替代方案是什么?

linux - 使用 message_queue 进行双向消息传递时 boost.interprocess 的问题

c++ - Windows 管理的 2 个窗口是否可能具有相同的窗口句柄值?

regex - 仅过滤文件中的大写单词

powershell - 跟踪我刚在PowerShell中启动的进程的PID

SQL Server Management Studio 2008 计划导出到 MS Access