Create scheduled task with complex trigger and gMSA - Catch 22 ! RRS feed

  • Question

  • Hi,

    We're adopting group managed service accounts to run services, scheduled tasks etc on our application servers (mix of Windows Server 2012 & 2016). We're using Powershell to define our task schedules, as the Task Scheduler GUI tool cannot be used to configure tasks to be run under a group managed service account.

    Unfortunately some of our scheduled tasks have relatively complex trigger logic - e.g. "run task hourly between 7am and 7pm every sunday, monday and tuesday". It is possible to configure these schedules in the Task Scheduler GUI tool - but not using Powershell.

    So... we can't define the tasks in the GUI tool, as it doesn't support group managed service accounts ... and we can't define the tasks in Powershell, as it doesn't support the creation of complex triggers. Catch 22 ! 

    Any ideas as to how we can successfully define these tasks?

    Wednesday, October 24, 2018 5:01 PM


All replies

  • You can use New-ScheduledTaskTrigger to build the set of triggers. Or build the task in the GUI, export to XML, and use the XML in your script to create the tasks on the other machines.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    • Marked as answer by Steve Blomeley Thursday, October 25, 2018 10:52 AM
    Wednesday, October 24, 2018 6:29 PM
  • Thanks for the suggestions Rich. The approach of using += to build a set of triggers looks promising - I hadn't realised that was possible. I'll give it a try today and report back later with results.
    Thursday, October 25, 2018 7:21 AM
  • So I basically hadn't realised that the Trigger parameter to the Register-ScheduledTask powershell command accepts an array. So the solution to my problem - as Rich pointed out - is to build a collection of triggers that together define the schedule that I need:

    $Action = <the action to be performed>
    $Principal = <the group managed service account to run the task>
    $Triggers = @()
    $Triggers += New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday,Monday,Tuesday -At 07:00
    $Triggers += New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday,Monday,Tuesday -At 08:00
    . . .
    $Triggers += New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday,Monday,Tuesday -At 18:00
    $Triggers += New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday,Monday,Tuesday -At 19:00
    Register-ScheduledTask -TaskName "Test" -Trigger $Triggers -Principal $Principal -Action $Action

    When I examine the newly created task in the Task Scheduler GUI tool, it now has the schedule I need, and is configured to run against the required group managed service account. Perfect!

    Thursday, October 25, 2018 10:52 AM