How to restrict windows tasks to weekdays only?
Asked Answered
G

2

10

I have a task that triggers on "user session logon". I now want to restrict that task to fire only on weekdays, and being ignored on the weekend.

Is that possible?

Sidenote: I cannot use the trigger on schedule as I do not want to run the task periodically, but only on logon, and only on weekdays.

Gaza answered 4/5, 2016 at 10:7 Comment(1)
superuser.com/questions/513676/…Cribwork
S
25
  1. click Weekly (NOT Daily)
  2. Choose the days you want
Sylvie answered 10/8, 2018 at 23:1 Comment(1)
But it should run only on logon during weekdays. Not just every weekday at a specific time.Resile
R
3

As far as I understand, this is not possible using the task scheduler alone.

You could use a piece of VBScript to achieve this.

Set up a file, e.g. mytask.vbs, like this:

If DatePart("w", Date, vbMonday) < 6 Then
    Set Shell = CreateObject("WScript.Shell")
    WScript.Quit(Shell.Run("C:\Windows\System32\notepad.exe", 10, True))
End If

Replace notepad by the task you actually want to run. What this things does is: It checks whether the current day is Mo-Fr (this is done by specifying the start of the week as Monday, so DatePart will return values from 1=Monday to 7=Sunday, and then we're checking if it's below 6), and if yes, it runs a certain program, waits for it to finish and forwards its exit code. (The magic number 10 here means that it will respect whatever setting for window display (normal, maximized, minimzed) was passed by the task scheduler, if any, and also forward it to the program.)

Then you can create a scheduled task with a logon trigger only, which runs wscript.exe /e:vbscript c:\path\to\your\mytask.vbs. That's it!

Resile answered 4/5, 2016 at 11:18 Comment(1)
Excellent solution if you want a repeating task to not run on weekends.Mechanistic

© 2022 - 2024 — McMap. All rights reserved.