1

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

1 Answer 1

1

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.

3
  • Hello, I found a solution by myself, buty our answer helped me to find it. I'll edit my question and post my own solution, so people can use it in the future. Commented Apr 3, 2018 at 11:01
  • @Twinfriends: Great to hear that. Commented Apr 3, 2018 at 11:01
  • 1
    Added my solution. Kinda strange that this works, but I'll take it ;) Commented Apr 3, 2018 at 11:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.