question

Johan-8824 avatar image
0 Votes"
Johan-8824 asked MotoX80 answered

Powershell: New-ScheduledTaskTrigger cmdlet AtLogon and Repetition

Goal: To create a Scheduled Task trigger on user logon with a repetition and a duration.

Example 01:

$ac = New-ScheduledTaskAction -Execute {write-host 'TEST'}
$tr = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration (New-TimeSpan -Minutes 30)
$ta = Register-ScheduledTask -TaskName "Test01" -Trigger $tr -Action $ac

This works fine.


Example 02:

$ac = New-ScheduledTaskAction -Execute {write-host 'TEST'}
$tr = New-ScheduledTaskTrigger -AtLogOn -RepetitionInterval (New-TimeSpan -Minutes 5) -RepetitionDuration (New-TimeSpan -Minutes 30)
$ta = Register-ScheduledTask -TaskName "Test02" -Trigger $tr -Action $ac

This don't work.


Example 03:

$ac = New-ScheduledTaskAction -Execute {write-host 'TEST'}
$tr = New-ScheduledTaskTrigger -AtLogOn
$ta = Register-ScheduledTask -TaskName "Test03" -Trigger $tr -Action $ac
$ta.Triggers.Repetition.Duration = "PT30M"
$ta.Triggers.Repetition.Interval = "PT5M"
$ta | Set-ScheduledTask

This works fine. But do you really need to create it first then update it to get it work? Can't you do it directly like in example 2?

windows-10-generalwindows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered

The trick appears to be that you need to copy the repetition from another trigger.

I noticed that the -AtLogOn would cause it to be executed at the logon of any user, but the runas was set to the author of the task, so it would fail.

To create the task when the author of the task logs in, use this. (If you're in a domain, you might also need to include that name.)

 unregister-scheduledtask -TaskName "Test02" -confirm:$false
 $ac = New-ScheduledTaskAction -Execute "cmd.exe"  -Argument "/c timeout.exe /t 5"
 $tr = New-ScheduledTaskTrigger -AtLogOn -User $env:USERNAME
 $tr.Repetition = (New-ScheduledTaskTrigger -once -at "12am" -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration (New-TimeSpan -Minutes 3)).repetition
 Register-ScheduledTask -TaskName "Test02" -Trigger $tr -Action $ac


To run the task at the logon of any user, use this.

 unregister-scheduledtask -TaskName "Test02" -confirm:$false -ErrorAction SilentlyContinue
 $ac = New-ScheduledTaskAction -Execute "cmd.exe"  -Argument "/c timeout.exe /t 5"
 $tr = New-ScheduledTaskTrigger -AtLogOn 
 $pr = New-ScheduledTaskPrincipal  -Groupid  "INTERACTIVE" 
 $tr.Repetition = (New-ScheduledTaskTrigger -once -at "12am" -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration (New-TimeSpan -Minutes 3)).repetition
 Register-ScheduledTask -TaskName "Test02" -Trigger $tr -Action $ac   -Principal $pr







5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.