Powershell: New-ScheduledTaskTrigger cmdlet AtLogon and Repetition

Johan 21 Reputation points
2021-09-30T21:33:20.98+00:00

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
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,617 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 31,571 Reputation points
    2021-10-01T14:58:57.343+00:00

    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
    
    0 comments No comments

0 additional answers

Sort by: Most helpful