Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: Scheduled Task with Daily Trigger and Repetition Interval

I cant seem to figure out how to create a new scheduled task that is triggered daily and repeats every 30 minutes. I have been going in circles.

Everything about this below works for setting the task I want, but only triggered once.

#Credentials to run task as $username = "$env:USERDOMAIN\$env:USERNAME" #current user $password = "notmypass"  #Location of Scripts: $psscript = "C:\test\test.ps1" $Sourcedir ="C:\testsource\" $destdir = "C:\testdest\" $archivepassword = "notmypass"   ####### Create New Scheduled Task $action = New-ScheduledTaskAction -Execute "Powershell" -Argument "-WindowStyle Hidden `"$psscript `'$sourcedir`' `'$destdir`' `'$archivepassword`'`"" $trigger = New-ScheduledTaskTrigger -Once -At 7am -RepetitionDuration  (New-TimeSpan -Days 1)  -RepetitionInterval  (New-TimeSpan -Minutes 30) $settings = New-ScheduledTaskSettingsSet -Hidden -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable $ST = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings Register-ScheduledTask EncryptSyncTEST -InputObject $ST -User $username -Password $password 

If I change -Once to -Daily I lose the -RepetitionInterval flags. And if I come back to update the task to daily after registering it, it wipes the repeating trigger.

This isn't an uncommon scheduling method, and is easily applied through the task scheduler UI. I feel like it is probably simple but I am missing it.

Any help is appreciated.

EDIT: Addressing the duplicate question. The question in the post "Powershell v3 New-JobTrigger daily with repetition" is asking the same. But as I commented earlier, none of the answers solve the issue. The marked answer does exactly what I already have here, it sets a task with a -Once trigger, then updates it to repeat every 5 minutes for 1 day. After the first day that task will never be triggered again. It does not address the issue of triggering a task everyday with repetition and duration until the next trigger.

The other three answers on that post are also not addressing the question. I do not know why it was marked answered, because it is not correct. I fully explored those replies before I posted this question. With that post having aged and being marked as answered I created this question.

Note: I have found a workaround, but not a great one. At current it seems the easiest way to define custom triggers using powershell is to manipulate the Scheduled Task XML and import it directly using Register-ScheduledTask

like image 384
malexander Avatar asked Nov 20 '13 22:11

malexander


1 Answers

While the PowerShell interface for scheduled task triggers is limited, it turns out if you set the RepetitionDuration to [System.TimeSpan]::MaxValue, it results in a duration of "Indefinitely".

$trigger = New-ScheduledTaskTrigger `     -Once `     -At (Get-Date) `     -RepetitionInterval (New-TimeSpan -Minutes 5) `     -RepetitionDuration ([System.TimeSpan]::MaxValue) 

Tested on Windows Server 2012 R2 (PowerShell 4.0)

like image 99
Shaddy Zeineddine Avatar answered Oct 04 '22 01:10

Shaddy Zeineddine