如何配置“如果任务已经在运行,则适用以下规则";在 Windows 任务计划程序中使用 PowerShell 脚本? [英] How to configure "If the task is already running, then the following rule applies" in Windows Task Scheduler using PowerShell script?

查看:55
本文介绍了如何配置“如果任务已经在运行,则适用以下规则";在 Windows 任务计划程序中使用 PowerShell 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 PowerShell 脚本实现以下设置(选择如果任务已在运行,则应用以下规则"),但无法获得适当的设置来进行配置.

I am trying to achieve the following settings (select "If the task is already running, then the following rule applies") through PowerShell script but unable to get appropriate settings to configure that.

我正在使用以下代码进行配置

$Trigger = New-ScheduledTaskTrigger -At 07:00am -Daily

$Settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Hour 1) -Compatibility Win7 -StartWhenAvailable -Priority 7

$User = "SYSTEM"

$Action = New-ScheduledTaskAction -Execute "some script" -Argument "some argument" -WorkingDirectory "working dir"

Register-ScheduledTask -TaskName "Test Task" -Trigger $Trigger -User $User -Action $Action -Settings $Settings -RunLevel Highest –Force

对触发器进行高级配置

$Task = Get-ScheduledTask -TaskName "Example Task"

$Task.Triggers[0].ExecutionTimeLimit = "PT10M"

$Task | Set-ScheduledTask -User $User

推荐答案

该设置通过 New-ScheduledTaskSettingsSet 并且您要查找的参数是 -MultipleInstances:

-MultipleInstances

指定定义任务计划程序如何处理多个任务实例的策略.此参数可接受的值为:

Specifies the policy that defines how Task Scheduler handles multiple instances of the task. The acceptable values for this parameter are:

忽略新的.新任务实例被忽略.平行.新任务实例立即启动.队列.新任务实例在当前实例完成后立即启动.

IgnoreNew. The new task instance is ignored. Parallel. The new task instance starts immediately. Queue. The new task instance starts as soon as the current instance completes.

类型:MultipleInstancesEnum
接受的值:ParallelQueueIgnoreNew
职位:命名
默认值:

Type: MultipleInstancesEnum
Accepted values: Parallel, Queue, IgnoreNew
Position: Named
Default value: None

然而,文档只列出了 3 个值,以及相应的枚举(至少在撰写本文时也只列出了 3 个值:

However, the documentation lists only 3 values, and the respective enum (at least at the time of this writing also only has the listed 3 values:

  • 并行并行运行一个新实例
  • 队列排队一个新实例
  • IgnoreNew不要启动新实例
  • ParallelRun a new instance in parallel
  • QueueQueue a new instance
  • IgnoreNewDo not start a new instance

如果您通过 GUI 手动创建任务并选择设置停止现有实例",则值 .Settings.MultipleInstances 为空,但如果您创建一个设置对象通过 New-ScheduledTaskSettingsSet 省略参数 -MultipleInstances 它默认为 IgnoreNew.尝试将其更改为空值会导致验证错误.

If you create a task manually via the GUI and select the setting "Stop the existing instance" the value .Settings.MultipleInstances is empty, but if you create a Settings object via New-ScheduledTaskSettingsSet omitting the parameter -MultipleInstances it defaults to IgnoreNew. Attempts to change that to an empty value result in validation errors.

这显然是一个错误(引用的枚举中缺少值).

This is obviously a bug (missing value in the referenced enum).

这篇关于如何配置“如果任务已经在运行,则适用以下规则";在 Windows 任务计划程序中使用 PowerShell 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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