设置New-ScheduledTaskTrigger的属性值

时间:2015-12-22 22:39:59

标签: powershell object scheduled-tasks windows-server-2016

我正在尝试使用PowerShell创建新的计划任务,但我无法更改其中一个属性:Repetition Property CimInstance#Instance Repetition {get;set;}

$action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-c test.ps1"
$trigger = New-ScheduledTaskTrigger -daily -at 10am -DaysInterval 1 
$trigger.DaysInterval = (New-TimeSpan -hours 1)
$trigger.enabled = $true
$trigger.Repetition = (New-TimeSpan -Days 1) #Failing with error

.....

Exception setting "Repetition": "The adapter cannot set the value of property "Repetition"."
At line:1 char:1
+ $trigger.Repetition = 1
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterSetValue

是否有与New-ScheduledTaskAction相关联的参数可用于通过cmdlet更改这些属性?

1 个答案:

答案 0 :(得分:1)

可以设置这些值,但不能设置New-ScheduledTaskActionNew-ScheduledTaskTrigger的重复间隔与参数集名称“Once”相关联。它与Daily参数集无关。

要使用这些参数,您需要使用-Once而不是-Daily定义不同的触发器。例如:

$hourly = (New-TimeSpan -Hours 1)
$days = (New-TimeSpan -Days 30)
$trigger = New-ScheduledTaskTrigger -Once -At 10am -RepetitionInterval $hourly -RepetitionDuration $days

这将定义一个从上午10点开始的触发器,并且每小时再次触发30天。