1

I am trying to schedule a task to execute every 5 minutes using powershell. The code below works but terminates after some time. Any ideas what might be causing that? Is it the code or should i look elsewhere? Thanks, Alex

$Ale = New-ScheduledTaskAction -Execute "C:\Users\User\revie\rev.exe"
$Tle = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 10)
$Ple = New-ScheduledTaskPrincipal "domain\User"
$Sle = New-ScheduledTaskSettingsSet
$Dle = New-ScheduledTask -Action $Ale -Trigger $Tle -Principal $Ple -Settings $Sle
Register-ScheduledTask meterp1 -InputObject $Dle
Alex
  • 23
  • 1
  • 6
  • Maybe `-Once` and `-RepetitionInterval` don't work nicely with each other? – vonPryz Mar 19 '20 at 06:57
  • basicly it does what you can do with the task scheduler, if you cant use once and repetitions at the same time there, you wont be able to in powershell either – Jim Wolff Mar 19 '20 at 07:23
  • things look good to me, maybe `meterp1` should be in quotes, which error are you getting? from docs it looks like you can use `-Once` and `-RepetitionInterval` just fine. `Get-Help New-ScheduledTaskTrigger` – Jim Wolff Mar 19 '20 at 07:31
  • There is no error ,, it works fine and then after some time it stops,, (btw it is on a remote machine with no access to the gui) – Alex Mar 19 '20 at 07:40
  • How long does it take before it stops working? – Bink Sep 14 '21 at 20:39

1 Answers1

-1

Use SchTasks.exe for this task:

schtasks.exe /create /tn "MyTask" /tr "C:\Users\User\revie\rev.exe"" /sc minute /mo 5 

SchTasks Docs: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/schtasks

  • /create specifies that a task will be created.

  • /tn is the task name.

  • /tr is the exe file path.

  • /sc is the time format.

Wasif
  • 14,755
  • 3
  • 14
  • 34