1

Not sure if this belongs in stackoverflow as it relates to PowerShell, but posted here as it isn't scripting and is more server task related.

I am trying to recreate a task we have on our old file server, as a Clustered task on our new failover cluster.

In order to do so, I need to set the action and trigger, as variables to pass to New-ClusteredScheduledTask. The action I have done without issue, but I am not sure how to run New-ScheduledTaskTrigger to correctly have it set to the last Tuesday of every month.

I can see there are Daily and Weekly switches, and also WeeklyInterval, but I don't want to rely on weekly intervals as that won't always fall on the last Tuesday of the month?

Can anyone advise how to recreate this trigger using a combination of the available switches and values?

0

i don't think this can be done purely with PowerShell cmdlets.

I would create a sample task in the GUI with the correct trigger and export it as XML.

Then use that XML to create a new task: Register-ScheduledTask -Xml

  • Can this be done as a clustered scheduled task though? – James Edmonds Feb 4 '18 at 18:01
  • Sorry, messed up the pasting of the link to the MS documentation: docs.microsoft.com/en-us/powershell/module/scheduledtasks/… – James Edmonds Feb 5 '18 at 10:36
  • It doesn't actually seem to work as expected though, as I have exported a task from our old file server, and then tried to register as a clustered scheduled task on the new cluster, and all I get is "Register-ClusteredScheduledTask: The task XML is malformed". Odd, considering I haven't even edited it. I also tried export an existing clustered task and importing it, but that also fails? – James Edmonds Feb 5 '18 at 10:39
  • Ok, turns out it's because I was passing the path to the XML file as the value for -Xml. After doing a get-contents I was able to pass the entire XML as a string to that parameter. – James Edmonds Feb 5 '18 at 10:46
1

I've tried to do something similar in the past; and I never found a way to do it using New-ScheduledTaskTrigger.

Ultimately I ended up adding the logic to do that check to my script so it only runs if there are less than 7 days left in the month and the current day is Tuesday.

If(((([datetime]::DaysInMonth((Get-Date).Year,(Get-Date).Month) - (Get-Date).Day)) -le 7) -And ((Get-Date).DayOfWeek -eq "Tuesday"))
{
   # Perform Task
} 
  • Thanks. This is a good suggestion. Does seem odd that Microsoft have not added the ability to do this to the PowerShell commandlets – James Edmonds Feb 4 '18 at 18:09
  • Am going to accept Peter's answer, purely because it is closer to what I would consider a "native" way of achieving the desired triggers. – James Edmonds Feb 4 '18 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.