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