Search code examples
powershellscheduled-taskswindows-server-2012-r2taskscheduler

Create a TaskScheduler Entry with PowerShell


Hello guys

I have a problem and I'm not sure how to solve it.

What I want:

I want to create a scheduled task with PowerShell. The task should be triggered every day, beginning at 05:55am, for the next 15 hours, be triggered every 30 minutes. So far so good.

What I tried:

I tried to use the New-ScheduledTaskTrigger command from PowerShell. But there seems to be a problem. I can't use the RepetitionDuration and RepetitionInterval with -daily. Here is the code I tried first:

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

This results in an error, since those two parameters can't be used with daily. As I found out by using google, they can only be used with -once. But I don't want to trigger the task once. I want to trigger it daily.

Possibilities I see:

  • Use daily, and create a new task for every single time I want to trigger it. That seems pretty stupid to me.

Actually I don't see any other possibilities. I'm not sure why I can't use those parameter with daily, since its possible to create exactly that task with the GUI. Maybe I also don't understand what "once" mean, but for me this means it gets triggered only on one day. The next day, the task won't be executed at all again.

I would be really happy if someone can help me out here. If you need any further information, feel free to ask.

Thank you.

EDIT:

I found a way how I can do what I was looking for:

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

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


Solution

  • In this scenario, I would be suggesting you to avoid using the default cmdlets and start using the COMObject for this.

    $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
    

    I have already made a full fledged script out of it. Please refer that HERE

    Hope it helps.