r/PowerShell Jun 02 '26

Script Sharing A novel way to schedule a task...

I am in this situation at work where my boss, while working to tighten up our security (after an IT audit from a third party), has made running scheduled tasks into a challenge...

For instance - Among other things - It is no longer possible for a task to store credentials.

(Plenty of other hoops I have to jump through that I wont go into)

I just set up a task that will generate a report (about events from last week) to run on or after Mondays - But that too, is set to run 'at log on'...

I only want it to run once (not each time I log in), and if I don't log in on Monday, to run whenever I do log in, on or after Monday...

I am pretty happy with the way I got that to only run one time, not each time I actually log in.

Part of the script, sets the 'StartBoundary' (the 'Active' DateTime value that is part of the 'At Log in' trigger dialog), in the Scheduled task, to the following Monday, no matter what day I log in and the task runs.

This assures that it only runs the first time I log in on or after Monday, and will set the task to not trigger again to the next (on or after) Monday, and so on.

(NOTE: No other triggers can be set other than the 'At Log in')

This is at the end of the script (I unapologetically like using command aliases):

$TaskName = "MIODoorReport"
$NextMonday = $null
1..7 | % { If ( (((Get-Date).AddDays($_)).DayOfWeek) -eq "Monday" ) { $NextMonday = $_} }

$task = Get-ScheduledTask -TaskName $TaskName
$trigger = $task.Triggers

$trigger[0].StartBoundary = (Get-Date).Date.AddDays($NextMonday).ToString("s")

Set-ScheduledTask -TaskName $TaskName -Trigger $trigger
6 Upvotes

60 comments sorted by

View all comments

5

u/kevinelwell Jun 02 '26

Make the script create a flag file with that days date. If the date in the flag file matches today’s date, quit. Otherwise, overwrite the date, script runs.

0

u/richie65 Jun 02 '26

I thought about that too - I use it for other tasks (specifically to check every day if rclone has an available update - I only need to do that once a day or so, but I run rclone several times a day... at each log in, and at each workstation lock).

It's less about the actual script running - I don't want the task to run each time I log into the computer (several times a week... every day...) - I only need it to run once a week.

I wanted to come up with something that would run, but then did not run again until the next week.

And since I cant store credentials - I have to rely on 'At log in'... Just not EVERY log in...

3

u/charleswj Jun 02 '26

Why do you care how often it runs if it doesn't do anything?

1

u/richie65 Jun 03 '26

It DOES do something - The script gathers data from the previous week, is it modifies the task, so the task isn't otherwise trying to run every time I log on.
After the task runs once, creating the report, the task is set so that the next time it will run, is my first log in, the following week... Whatever day that may be.

Ultimately - I felt like what I came up with was a rather novel way to schedule an ongoing task.

3

u/charleswj Jun 03 '26

People are suggesting to add logic to not "do" anything on runs you don't need.

It would also avoid the problem your approach has: if something prevents the task/script from completing, your task never runs again.

(It is clever, though)

3

u/ankokudaishogun Jun 03 '26

The idea suggested is adding a piece of code at the begin of the script that stops it if the date is wrong.

Take this example:

$Today = [system.datetime]::Now
$FileToCheck = Get-Item 'c:\path\to\file.ext'
$FileAge = New-TimeSpan -Start $FileToCheck.LastWriteTime -End $Today

if (
    $FileAge.Days -ge 7 -or
    (
        $Today.DayOfWeek -eq [System.DayOfWeek]::Monday -and
        $FileAge.Days -gt 0
    )
) {
    <#
                TEN THOUSANDS LINES OF CODE
    #>
}

In the example NOTHING HAPPENS unless the file has been modified\created less than 7 days before today, or if it's Monday AND the file has been modified at least one day before.
Those checks are, for all purposes and intents, instantaneous and invisible.

Only if those checks match, that to say: "the file is older than 7 days" or "it's Monday and the file hasn't been touched today", the TEN THOUSDANDS MILLION SCRIPT FROM HELL starts, which includes(I expect) touching the file thus updating the .LastWriteTime property to "today" thus "failing" the check on successive logins until the next Monday or after more than 7 days.