1

I'm trying to add a Powershell script to task scheduler to run on a regular basis.

Import-Module ScheduledTasks

$scriptFilePath = "C:\hello-there.ps1"
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File $scriptFilePath -Noninteractive"

$repetitionDuration = New-TimeSpan -End (Get-Date -Year 2030 -Month 4)
$repetitionInterval = New-TimeSpan -Minute 15

$trigger = New-ScheduledTaskTrigger -RepetitionDuration $repetitionDuration -RepetitionInterval $repetitionInterval
Register-ScheduledTask -TaskName "custom-task" -Description "Does something important" -Trigger $trigger -Action $action

It executes until New-ScheduledTaskTrigger line, then asks me for user input? "Supply values for the following parameters"
What am I doing wrong?

chester89
  • 111
  • 6
  • You aren't specifying enough parameters for New-ScheduledTaskTrigger. The host is asking you to supply values for the missing mandatory parameters. – Mike Shepard Apr 23 '18 at 14:28
  • @MikeShepard which ones? I read documentation - At, Daily, AtStartup, AtLogon are use-case specific – chester89 Apr 23 '18 at 14:32

2 Answers2

2

Looking at the help for New-ScheduledTaskTrigger (for the ParameterSet which includes the 2 -Repetition* parameters you have specified) you can see that -At is not in brackets, so it's mandatory.

New-ScheduledTaskTrigger
   [-RandomDelay <TimeSpan>]
   -At <DateTime>
   [-Once]
   [-RepetitionDuration <TimeSpan>]
   [-RepetitionInterval <TimeSpan>]
   [-CimSession <CimSession[]>]
   [-ThrottleLimit <Int32>]
   [-AsJob]
   [<CommonParameters>]
Mike Shepard
  • 748
  • 3
  • 7
0

It turns out you need to specify -Once -At (Get-Date) to make it work (I found a clue in an answer here)

chester89
  • 111
  • 6