Powershell Scheduled Task At Startup Repeating
Asked Answered
S

4

5

I'm trying to create a Scheduled Task with the following Trigger:
- Startup
- Run every 5 minutes
- Run indefinitely

In the GUI I can do this easily by selecting:
- Begin the task: at startup
And in the Advanced tab:
- Repeat task every: 5 minutes
- For a duration of: indefinitely

But I'm having trouble doing it with Powershell.

My troubled code:
$repeat = (New-TimeSpan -Minutes 5)
$duration = ([timeSpan]::maxvalue)
$trigger = New-ScheduledTaskTrigger -AtStartup -RepetitionInterval $repeat -RepetitionDuration $duration

It won't take the RepetitionInterval and RepetitionDuration parameters. But I need that functionality. How could I accomplish my goal?

Stereotomy answered 2/12, 2017 at 19:49 Comment(0)
N
5

To set the task literally with a "Startup" trigger and a repetition, it seems you have to reach in to COM (or use the TaskScheduler UI, obviously..).

# Create the task as normal
$action = New-ScheduledTaskAction -Execute "myApp.exe"
Register-ScheduledTask -Action $action -TaskName "My Task" -Description "Data import Task" -User $username -Password $password

# Now add a special trigger to it with COM API.
# Get the service and task
$ts = New-Object -ComObject Schedule.Service
$ts.Connect()
$task = $ts.GetFolder("\").GetTask("My Task").Definition

# Create the trigger
$TRIGGER_TYPE_STARTUP=8
$startTrigger=$task.Triggers.Create($TRIGGER_TYPE_STARTUP)
$startTrigger.Enabled=$true
$startTrigger.Repetition.Interval="PT10M" # ten minutes
$startTrigger.Repetition.StopAtDurationEnd=$false # on to infinity
$startTrigger.Id="StartupTrigger"

# Re-save the task in place.
$TASK_CREATE_OR_UPDATE=6
$TASK_LOGIN_PASSWORD=1
$ts.GetFolder("\").RegisterTaskDefinition("My Task", $task, $TASK_CREATE_OR_UPDATE, $username, $password, $TASK_LOGIN_PASSWORD)
Nessy answered 16/9, 2018 at 23:58 Comment(1)
Great example! Also, it is possible to avoid this COM approach, see my answer here: https://mcmap.net/q/2032873/-use-powershell-to-add-property-quot-on-workstation-unlock-quot-to-scheduled-taskNorfolk
B
3

New-ScheduledTaskTrigger uses parameter sets. When you specify that you want the scheduled task to start up "at logon" you are restricting yourself to the following parameter set:

Parameter Set: AtStartup
New-ScheduledTaskTrigger [-AtStartup] [-RandomDelay <TimeSpan> ] [ <CommonParameters>]

What may be more beneficial is if you use your "at startup" scheduled task to register a new scheduled task to run every five minutes using the "once" parameter set:

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

Your Scheduled Task Trigger should successfully be assigned once you are using the correct parameter set.

Bussey answered 4/12, 2017 at 15:36 Comment(1)
any example to get inspired from ?Footwall
N
0

See my answer here: https://mcmap.net/q/2032873/-use-powershell-to-add-property-quot-on-workstation-unlock-quot-to-scheduled-task

It is actually relatively simple, once a working example is written, of course.

Instead of -AtLogOn use -AtStartup.

Norfolk answered 7/5, 2020 at 9:49 Comment(0)
E
0

For what it’s worth, I too ran into this issue as I was particularly perturbed that this functionality was pulled from PowerShell for -AtStartup. However, upon further review, the same functionality can be performed by just be using -Once and configuring it to repeat. In the OP’s case of having this run every five minutes, it probably won’t make a difference if this is run at startup or five minutes thereafter.

I have confirmed this with -Once and then configuring it to repeat. It will also perform the repeats when the system is restarted. This is somewhat different from @BryceMcDonald’s answer in that you don’t need to create two triggers—just create the -Once one and you’ll be good to go.

Essentialism answered 13/9, 2021 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.