1

Related to this question:

Windows Server in UTC, need Task Scheduler to shift tasks with summer time

and this question:

https://stackoverflow.com/questions/11458029/enabling-disabling-tasks-in-task-scheduler-using-a-powershell-script

Is there a way to selectively enable and disable certain triggers (not the whole task) with powershell?

I have two "Daily" triggers on my task on my UTC server, one scheduled to run an hour after the other. I only want one to be enabled, and I want to toggle between them twice a year.

The Task Scheduler doesn't appear to have a unique "name" for the triggers, they just appear as "Daily"

Cade Roux
  • 375
  • 2
  • 5
  • 18
  • 1
    If your tasks really depend on local time, then run the server on local time. – Michael Hampton Feb 17 '13 at 19:00
  • @MichaelHampton Some tasks do, some don't. I certainly don't want to run my server on local time, since events which need to happen at 2am don't need to happen twice when the time changes. – Cade Roux Feb 17 '13 at 21:30

2 Answers2

2

There are lots of ways to interface with Task Scheduler. For example, you can use the Schedule.Service COM object.

$TaskScheduler = New-Object -COMObject Schedule.Service
$TaskScheduler.Connect()
$TaskFolder = $TaskScheduler.GetFolder("\") # If your task is in the root "folder"
$Task = $TaskFolder.GetTask("The Name Of Your Task")
$Triggers = $Task.Definition.Triggers
$FirstTrigger = $Triggers.Item(1)

Those trigger objects each have an Enabled property. I think the array indices here start at 1, which is weird, but whatever.

Edit: And oh yeah, the RegisterTaskDefinition() method should save your changes.

Ryan Ries
  • 55,481
  • 10
  • 142
  • 199
0

I wanted to disable a trigger that runs at user logon. Piggybacking off the other answer, here's a complete example.

$ts = New-Object -Com Schedule.Service
$ts.Connect()
$dir = $ts.GetFolder('\')
$task = $dir.GetTask("ENTER NAME HERE")
$def = $task.Definition
# Logon trigger -> type 9
$disable = $def.Triggers | ? {$_.Type -eq 9}
$disable.Enabled = $false
# 4 means update an existing task
$dir.RegisterTaskDefinition($task.Name, $def, 4, $null, $null, $null)
Bangaio
  • 160
  • 1
  • 8