'Task scheduler to run tasks only during the last full workweek of the month

I have multiple tasks that need to be scheduled during the last full workweek (Monday through Friday) of every month. These will be scheduled from a Windows Server 2016 server. I cannot figure out the logic to do it.

Additional info to help: July 2020 is a perfect month to just say run on the last Monday/Tuesday/etc. Most months will not end with a full workweek though. It may end on a Tuesday and the full workweek ended 4 days before that.

I have ideas of how to do this, but they are not fully formulated:

  1. Find the last Friday of the month and start 4 days before that.
  2. Find the last Monday with at least 4 additional days in the month after.
  3. If the last day of the month is Sunday, start 6 days before that; Saturday, start 5 days before that; etc.

What is the cleanest method to do this in task scheduler? (The scripts being scheduled are in PowerShell, though I don’t think that matters).



Solution 1:[1]

So, your situation is: "I have a script, I have a complicated date schedule, and I would like my script to be launched according to that schedule.",
and your question is: "How to perform this scheduling in Windows task scheduler?"

My answer would be:
Don't do it like that, but write a new script which verifies the current date, and verifies if the current date corresponds to the complicate date schedule (in that case, launch your original script), and use Windows task scheduler to launch this new script on a daily basis.

As for making that new script, this URL explains how to perform date handling in Powershell.

Solution 2:[2]

This particular use case can actually be done fully in the Task Scheduler. When adding a new trigger, select Monthly then for Months select <Select all months>. Then, change the radio button below it to Onand chooseLastand`.

Selecting All Months

Selecting Last

Selecting All Days

Solution 3:[3]

May I suggest a solution:

In VBA write a very short Sub routine indicating the current date (using "Now() + 1". Then use "instr(1,string1,""/"")" to see whether the second entry in the resulting date (such as "5/1/2022") is a "1". If it is, then you can issue a warning on a prepared Notepad.txt the day before the 1st of the month that also provides an answer to your question regarding scheduling an event on the 1st of every month. The routine is to run (in the background) every day until it reaches this particular occurrence.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dominique
Solution 2 Kade
Solution 3 Friedemann Schaefer