使用 PowerShell 创建 TaskScheduler 条目

Create a TaskScheduler Entry with PowerShell

大家好

我有一个问题,我不知道如何解决它。

我想要的:

我想用 PowerShell 创建计划任务。该任务应每天触发,从 05:55am 开始,在接下来的 15 小时内,每 30 分钟触发一次。到目前为止一切顺利。

我试过的:

我尝试使用 PowerShell 中的 New-ScheduledTaskTrigger 命令。但是好像有问题。我无法将 RepetitionDurationRepetitionInterval-daily 一起使用。这是我首先尝试的代码:

$taskTrigger = New-ScheduledTaskTrigger -Daily -At 05:55am -RepetitionDuration (New-TimeSpan -Hours 15) -RepetitionInterval (New-TimeSpan -Minutes 30) 

这会导致错误,因为这两个参数不能与 daily 一起使用。正如我通过使用 google 发现的那样,它们只能与 -once 一起使用。但是我不想触发一次任务。我想每天触发它。

我看到的可能性:

其实我看不出任何其他的可能性。我不确定为什么我不能每天使用这些参数,因为可以使用 GUI 准确地创建该任务。也许我也不明白 "once" 是什么意思,但对我来说这意味着它只会在一天内触发。第二天,任务将不再执行。

如果有人能在这里帮助我,我会很高兴。如果您需要任何进一步的信息,请随时询问。

谢谢。

编辑:

我找到了一种方法可以做我正在寻找的事情:

Register-ScheduledTask -Action $taskAction -TaskName $taskName -Trigger $taskTrigger -User $SAUserName -Password $SAPassword -TaskPath "\Citrix MGMT"
Start-Sleep -Seconds 3
$task = Get-ScheduledTask -TaskName "TestTask"
$task.Triggers.repetition.Duration = 'PT15H'
$task.Triggers.repetition.Interval = 'PT30M'
$task | Set-ScheduledTask -User $SAUserName -Password $SAPassword

参考: https://www.petri.com/creating-repeating-powershell-scheduled-jobs https://msdn.microsoft.com/en-us/library/windows/desktop/aa382119%28v=vs.85%29.aspx

在这种情况下,我建议您避免使用默认的 cmdlet 并为此开始使用 COMObject。

$service = new-object -ComObject("Schedule.Service")
                        # connect to the local machine.

                        $service.Connect()
                        $rootFolder = $service.GetFolder("\")
                        $TaskDefinition = $service.NewTask(0)
                        $TaskDefinition.RegistrationInfo.Description = "$TaskDescr"
                        $TaskDefinition.Settings.Enabled = $true
                        $TaskDefinition.Settings.AllowDemandStart = $true
                        $TaskDefinition.Settings.StartWhenAvailable = $true
                        $TaskDefinition.Settings.StopIfGoingOnBatteries=$false
                        $TaskDefinition.Settings.DisallowStartIfOnBatteries=$false
                        $TaskDefinition.Settings.MultipleInstances=2
                        $taskdefinition.Settings.WakeToRun=$true
                        $triggers = $TaskDefinition.Triggers
                        $trigger = $triggers.Create(1) # Creates a "One time" trigger
                        $trigger.StartBoundary = $TaskStartTime.ToString("yyyy-MM-dd'T'HH:mm:ss")
                        $time_interval=New-TimeSpan -Minutes $interval
                        $time_interval=$time_interval.TotalSeconds
                        $trigger.Repetition.Interval= "PT"+"$time_interval"+"S"
                        $trigger.Enabled = $true
                        $TaskDefinition.Principal.RunLevel =1
                        $Action = $TaskDefinition.Actions.Create(0)
                        $action.Path = "$TaskCommand"
                        $action.Arguments = "$TaskArg"
                        # In Task Definition,
                        #   6 indicates "the task will not execute when it is registered unless a time-based trigger causes it to execute on registration."
                        #   5 indicates "Indicates that a Local System, Local Service, or Network Service account is being used as a security context to run the task.In this case, its the SYSTEM"
                        $rootFolder.RegisterTaskDefinition("$TaskName",$TaskDefinition,6,"System",$null,5) | Out-Null

我已经用它制作了一个完整的脚本。请参考 HERE

希望对您有所帮助。