Powershell New-ScheduledTaskTrigger使用-RepetitionInterval重复执行任务

时间:2014-09-15 18:26:14

标签: powershell scheduled-tasks eventtrigger

我发现与ScheduledTask cmdles相关的错误会产生极其神秘的错误。 我想安排一个测试任务每1分钟运行一次并永远重复。 这是我尝试过的,但没有成功:

cls
#$Action = New-ScheduledTaskAction -Execute "e:\Scripts\TestClusteredTask.cmd"
$DurationTimeSpan = New-TimeSpan -Minutes 1 
$DurationTimeSpanIndefinite = ([TimeSpan]::MaxValue) 
$DurationTempTest = New-TimeSpan -Days 1000
$Trigger = New-ScheduledTaskTrigger -Daily -At "00:01" -RepetitionInterval $DurationTimeSpan -RepetitionDuration $DurationTempTest  

也尝试了这个:

$Trigger = New-ScheduledTaskTrigger -Daily -At "00:01" -RepetitionInterval $DurationTimeSpan -RepeatIndefinitely 

错误输出:

New-ScheduledTaskTrigger : Parameter set cannot be resolved using the specified named parameters.
At C:\Users\nwalters\Documents\AddTestClusteredTask.ps1:6 char:12
+ $Trigger = New-ScheduledTaskTrigger -Daily -At "00:01" -RepetitionInterval $Dura ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-ScheduledTaskTrigger], ParameterBindingException
    + FullyQualifiedErrorId : AmbiguousParameterSet,New-ScheduledTaskTrigger

我引用此网页获取说明。 http://technet.microsoft.com/en-us/library/hh849759.aspx

Win 2012 / R2上的Powerhsell第4版

4 个答案:

答案 0 :(得分:4)

根据tech net,这是您正在使用的数据集

Parameter Set: Daily
New-ScheduledTaskTrigger [-Daily] -At <DateTime> [-DaysInterval <Int32> ] [-RandomDelay <TimeSpan> ] [ <CommonParameters>]

在您的示例中,您指定的-RepetitionInterval-RepetitionDuration-Once参数集的一部分。

Parameter Set: Once
New-ScheduledTaskTrigger [-Once] -At <DateTime> [-RandomDelay <TimeSpan> ] [-RepetitionDuration <TimeSpan> ] [-RepetitionInterval <TimeSpan> ] [ <CommonParameters>]

这就是为什么你会出现歧义错误的原因。它不知道您正在使用哪个参数集。

FullyQualifiedErrorId : AmbiguousParameterSet,New-ScheduledTaskTrigger

因此,如果要使用其他参数,则需要指定-Once

答案 1 :(得分:2)

我发现你可以通过多个步骤完成这项工作,例如:

$ST_A = New-ScheduledTaskAction –Execute "E:\Scheduled Tasks\pause.cmd"
$ST_T = New-ScheduledTaskTrigger -Once -At 0am
Register-ScheduledTask -TaskName "Pause_with_asJob2" -Action $ST_A –Trigger $ST_T
Disable-ScheduledTask -Taskname "Pause_with_asJob2"
$ST_T = New-ScheduledTaskTrigger -Once -At 03:00 -RepetitionDuration (new-timespan -hour 15) -RepetitionInterval (new-timespan -minutes 15)
Set-ScheduledTask -TaskName "Pause_with_asJob2" -Action $ST_A –Trigger $ST_T
$ST_T = New-ScheduledTaskTrigger -Daily -At 03:00
Set-ScheduledTask -TaskName "Pause_with_asJob2" -Action $ST_A –Trigger $ST_T

答案 2 :(得分:1)

我发现您可以在将Trigger链接到任务之前修改Trigger。 我的脚本每天8点钟生成一个带触发器的任务。任务每60分钟(1小时)重复一次,随机延迟30分钟:

$Prog = $env:systemroot + "\system32\WindowsPowerShell\v1.0\powershell.exe"
$Opt = "-nologo -noninteractive -noprofile -ExecutionPolicy BYPASS -file \\mydomain.local\NETLOGON\Monitoring\monitoring.ps1"
$Action = New-ScheduledTaskAction -Execute $Prog -Argument $Opt 
$Trigger = New-ScheduledTaskTrigger -Daily -DaysInterval 1 -RandomDelay "00:30" -At "08:00"
$Trigger.Repetition = $(New-ScheduledTaskTrigger -Once -RandomDelay "00:30" -At "08:00" -RepetitionDuration "12:00" -RepetitionInterval "01:00").Repetition
$Principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
$Task = Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -TaskName "Monitor-PS1-V001" -Description "Starts Monitoring-Script" -Force

我用Google搜索,发现没有好的解决方案。 希望有人可以使用我的想法&#34;

问候 罗伯特

答案 3 :(得分:0)

可能这可以帮助别人!!

$script="restart_Service"
$trigger=New-JobTrigger -Daily -At 3:00PM
$action=New-ScheduledTaskAction -Execute C:\temp\restart.ps1
Register-ScheduledTask -Action $action -TaskName $script -Description "to restart the service"  -RunLevel Highest -Trigger $trigger
$Task.Triggers.Repetition.Duration = "P1D" 
$Task.Triggers.Repetition.Interval = "PT30M"
$Task | Set-ScheduledTask -User "NT AUTHORITY\SYSTEM"
$Task = Get-ScheduledTask -TaskName "restart_Service"
$Task.Triggers.Repetition | fl *