Powershell 计划任务在启动时重复 [英] Powershell Scheduled Task At Startup Repeating

查看:106
本文介绍了Powershell 计划任务在启动时重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下触发器创建计划任务:
- 启动
- 每 5 分钟运行一次
- 无限期运行

I'm trying to create a Scheduled Task with the following Trigger:
- Startup
- Run every 5 minutes
- Run indefinitely

在 GUI 中,我可以通过选择:
- 开始任务:在启动时
在高级"选项卡中:
- 每重复一次任务:5 分钟
- 持续时间:无限期

In the GUI I can do this easily by selecting:
- Begin the task: at startup
And in the Advanced tab:
- Repeat task every: 5 minutes
- For a duration of: indefinitely

但是我在使用 Powershell 时遇到问题.

But I'm having trouble doing it with Powershell.

我的问题代码:
$repeat = (New-TimeSpan -Minutes 5)
$duration = ([timeSpan]::maxvalue)
$trigger = New-ScheduledTaskTrigger -AtStartup -RepetitionInterval $repeat -RepetitionDuration $duration

它不会采用 RepetitionInterval 和 RepetitionDuration 参数.但我需要那个功能.我怎样才能实现我的目标?

It won't take the RepetitionInterval and RepetitionDuration parameters. But I need that functionality. How could I accomplish my goal?

推荐答案

要使用启动"触发器和重复设置字面上的任务,似乎您必须访问 COM(或使用 TaskScheduler UI,显然..)

To set the task literally with a "Startup" trigger and a repetition, it seems you have to reach in to COM (or use the TaskScheduler UI, obviously..).

# Create the task as normal
$action = New-ScheduledTaskAction -Execute "myApp.exe"
Register-ScheduledTask -Action $action -TaskName "My Task" -Description "Data import Task" -User $username -Password $password

# Now add a special trigger to it with COM API.
# Get the service and task
$ts = New-Object -ComObject Schedule.Service
$ts.Connect()
$task = $ts.GetFolder("\").GetTask("My Task").Definition

# Create the trigger
$TRIGGER_TYPE_STARTUP=8
$startTrigger=$task.Triggers.Create($TRIGGER_TYPE_STARTUP)
$startTrigger.Enabled=$true
$startTrigger.Repetition.Interval="PT10M" # ten minutes
$startTrigger.Repetition.StopAtDurationEnd=$false # on to infinity
$startTrigger.Id="StartupTrigger"

# Re-save the task in place.
$TASK_CREATE_OR_UPDATE=6
$TASK_LOGIN_PASSWORD=1
$ts.GetFolder("\").RegisterTaskDefinition("My Task", $task, $TASK_CREATE_OR_UPDATE, $username, $password, $TASK_LOGIN_PASSWORD)

这篇关于Powershell 计划任务在启动时重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆