Powershell v3附带了所有这些新的作业调度cmdlet。它们看起来很棒,但是我在创建特定触发器时遇到了麻烦。我需要每天运行一份工作,每小时重复一次,持续一段时间。
使用Task-Scheduler UI非常简单,但我找不到New-JobTrigger cmdlet的方法。
如果我使用Daily参数集,我没有Repetition选项:
New-JobTrigger [-Daily] -At <DateTime> [-DaysInterval <Int32> ] [-RandomDelay <TimeSpan> ] [ <CommonParameters>]
如果我使用Once参数集,我就不能使用Daily选项
New-JobTrigger [-Once] -At <DateTime> [-RandomDelay <TimeSpan> ] [-RepetitionDuration <TimeSpan> ] [-RepetitionInterval <TimeSpan> ] [ <CommonParameters>]
我需要的,但显然不起作用,是两者之间的混合。例如:
New-JobTrigger -Daily -At xxxx -RepetitionDuration (New-TimeSpan -Hours 5) -RepetitionInterval (New-TimeSpan -Hours 1)
有可能吗?也许为同一份工作混合几个触发器?
答案 0 :(得分:5)
这对我有用:
注意 - 我知道原始问题与New-JobTrigger有关 目的是提供可翻译的可能解决方案 鉴于此问题适用于两者,也适用于New-JobTrigger。
$A = New-ScheduledTaskAction -execute "powershell" -argument "-nologo -noprofile -noninteractive C:\MyScript.ps1"
$T = New-ScheduledTaskTrigger -Once -At 03:00AM
Register-ScheduledTask -TaskName StartBasicServices_Local -Trigger $T -Action $A -description "MyScript" -User "NT AUTHORITY\SYSTEM" -RunLevel 1
$T.RepetitionInterval = (New-TimeSpan -Minutes 5)
$T.RepetitionDuration = (New-TimeSpan -Days 1)
Set-ScheduledTask StartBasicServices_Local -Trigger $T
答案 1 :(得分:1)
我整天都在研究这个问题,我终于得到了答案。你不能。无论如何不使用这些cmdlet。起初我认为这可能是一种解决方法:
$Job = Register-ScheduledJob -Name "YourJobName" {Gci};
$RepeatTrigger = New-JobTrigger -Once -At (Get-Date 10:00).ToShortTimeString() - RepetitionInterval (New-TimeSpan -Hours 1) -RepetitionDuration (New-TimeSpan -Hours 7);
$RepeatTrigger.Frequency = [Microsoft.PowerShell.ScheduledJob.TriggerFrequency]::Daily;
Add-JobTrigger -InputObject $Job -Trigger $RepeatTrigger
然而,它没有。使用trace-command看起来它会根据参数集验证属性。使用schtasks时也是如此:
schtasks /create /tn YourTask /tr notepad.exe /sc Hourly
它将每小时创建它,但它必须是一次性的任务。根据事物的外观,有一个用于COM API的.Net包装器,如果你刚接触PoSH,这可能是你最好的选择。您可以在CodePlex上找到它。
答案 2 :(得分:1)
我知道Add-JobTrigger,但我不明白如何实现我的 具体要求:每天上班,每小时重复一次, 持续一段时间。例如:每天上午10点重复 每1小时一次,直到下午5点。 - scarmuega 10月7日&12;在18:14
使用Register-ScheduledJob创建作业。 然后添加每日触发器。 如果我们能做到这一点会很棒,但是-Daily在PowerShell 4.0中不起作用。 继续阅读,我有另一种选择。
# Technet: http://technet.microsoft.com/en-us/library/hh849759.aspx
# To create a repeating schedule, use the Once parameter with the RepetitionDuration and RepetitionInterval parameters.
# -RepetitionInterval Repeats the job at the specified time interval. For example, if the value of this parameter is 2 hours, the job is triggered every two hours.
# The default value, 0, does not repeat the job.
# -RepetitionDuration Repeats the job until the specified time expires. The repetition frequency is determined by the value of the RepetitionInterval parameter.
# For example, if the value of RepetitionInterval is 5 minutes and the value of RepetitionDuration is 2 hours, the job is triggered every five minutes for two hours.
#
$trigger = New-JobTrigger -Daily -At 10:00 -RepetitionDuration 7:00:00 -RepetitionInterval 1:00:00 -DaysofWeek
Register-ScheduledJob -Name Test -Trigger $trigger -ScriptBlock {}
您可以组织您的工作,使其每小时都有一个触发器。
$trigger4am = New-JobTrigger -Daily -At 04:10
Register-ScheduledJob -Name ArchiveProd -Trigger $trigger4am -ScriptBlock {E:\ArchiveProd.ps1}
$trigger8am = New-JobTrigger -Daily -At 08:10
$trigger12pm = New-JobTrigger -Daily -At 12:10
$trigger6pm = New-JobTrigger -Daily -At 18:10
Add-JobTrigger -Trigger $trigger8am -Name ArchiveProd
Add-JobTrigger -Trigger $trigger12pm -Name ArchiveProd
Add-JobTrigger -Trigger $trigger6pm -Name ArchiveProd
希望这就是你所需要的。虽然我更喜欢添加Daily参数,但它对我的情况很有用。也许有人可以为我改进这个?
答案 3 :(得分:0)
尝试使用Add-JobTrigger
将多个作业触发器添加到预定作业中。
答案 4 :(得分:0)
clear
Import-Module TaskScheduler
# Create a new task
$task = New-Task
Add-TaskAction -Task $task -Path "C:\Windows\notepad.exe"
Add-TaskTrigger -Task $task -Daily -At "06:00" -Repeat "05:00" -For "12:00"
# Register the task
Register-ScheduledTask -Name "PowerShell - MyScript.ps1" -Task $task
Get-ScheduledTask PowerShell* | select -Property Name
# Remove-Task -n Power*
答案 5 :(得分:0)
答案很晚,但我从这个问题开始,然后进行了更多搜索,并在this link, Petri.com找到了解决方案
$principal = New-ScheduledTaskPrincipal -UserID MyDomain\MyGMSA$ -LogonType Password -RunLevel Highest
$action = New-ScheduledTaskAction "E:\DoSomething"
$when = New-ScheduledTaskTrigger -Daily -At "00:02" -DaysInterval 1
Register-ScheduledTask -TaskName MyNewTask–Action $action –Principal $principal -Trigger $when
$t = Get-ScheduledTask -TaskName MyNewTask
$t.Triggers.repetition.Interval = "PT15M" #every 15 mins
$t.Triggers.repetition.Duration = "PT7H30M" #for 7 hours 30
$t | Set-ScheduledTask
答案 6 :(得分:0)
您可以创建多个触发器,请参阅以下内容:
$job = Register-ScheduledJob -Name "Test1" -FilePath "C:\temp\script.ps1"
$triggers = New-Object System.Collections.Generic.List[Microsoft.PowerShell.ScheduledJob.ScheduledJobTrigger]
$times = ("08:00", "09:00", "10:00", "11:00", "12:00", "14:00", "15:00", "16:00", "17:00", "19:00", "22:00")
foreach($t in $times){
$triggers.Add((New-JobTrigger -Daily -At $t))
}
$job.AddTriggers($triggers, $true)
或更容易(但更慢)
$MyJob = Register-ScheduledJob -Name "MyJob" -FilePath "C:\temp\test.ps1"
$times = ("08:00", "09:00", "10:00", "11:00", "12:00", "14:00", "15:00", "16:00", "17:00", "19:00", "22:00")
foreach($t in $times){$MyJob | Add-JobTrigger -trigger (New-JobTrigger -Daily -At $t)}
答案 7 :(得分:0)
当您使用“Register-ScheduledJob”创建计划作业时,您无法配置每日执行和重复间隔。即使您手动修改 ScheduledJobTrigger 对象的“RepetitionInterval”和“RepetitionDuration”属性,触发器也将配置为每天执行一次或随重复执行一次。
我设法通过 Get-ScheduledTask
方法获取作业的相应任务并修改那里的触发器来配置它:
$trigger = New-JobTrigger -Weekly -WeeksInterval 1 -At "06:00" -DaysOfWeek Monday,Tuesday,Wednesday,Thursday,Friday Register-ScheduledJob -Name $cronjobName -Trigger $trigger -ScriptBlock $scriptBlock $task = Get-ScheduledTask -TaskName $cronjobName $trigger = $task.Triggers $trigger.Repetition.Interval = "PT10M" $trigger.Repetition.Duration = "PT12H" Set-ScheduledTask -TaskPath $task.TaskPath -TaskName $cronjobName -Trigger $trigger