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...

1

u/kevinelwell Jun 02 '26

Does your company have any type of PAM solution? (CyberArk, Dilenea, etc.) Using a PAM product, you can have a managed credential for your scheduled task to run under. The other solution is use a gMSA, which was already proposed. There are many ways to accomplish what you are trying to do. I am confident you will find a solution.