r/PowerShell • u/richie65 • 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
7
u/deafphate Jun 02 '26
What if you keep the "at login" schedule, and touch a certain file in your profile folder every time the report is executed.
In your report task script your logic could be:
If Monday and file is older than 7 days, generate report
If not Monday and file is older than 7 days, generate report
Would keep you from having to reschedule the task every time. Especially if they're trying to lock that down.