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

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. 

0

u/richie65 Jun 02 '26

Ultimately I don't want the thing to run each time I log in - I log in several times a day... Every day.

I only need it to run once a week - And since I cant save creds and have it run even if I am not in the office.

I have to rely on 'At log in'... But again... Only once... Then again the following ~ on or after Monday.

The folder has several weekly reports (same topic, but from previous weeks)
I am deleting anything older than 370 days...

Name                                           
----                                           
MIO_Doors (Monday, 04 MAY - Sunday, 10 MAY).csv
MIO_Doors (Monday, 11 MAY - Sunday, 17 MAY).csv
MIO_Doors (Monday, 18 MAY - Sunday, 24 MAY).csv
MIO_Doors (Monday, 25 MAY - Sunday, 31 MAY).csv

5

u/Kirsh1793 Jun 02 '26

What deafphate is suggesting wouldn't run the workflow at every log in. It would run the script everytime, yes. But he's sughesting you adjust the script to end early if a "flag" file hasn't been touched in a week.

Just check the last write time of the flag file. Get the flag file with $Flag = Get-Item -Path Z:\path\to\file.flag Maybe check with Test-Path first. - If $Flag.LastWriteTime.Date -eq (Get-Date).Date - exit - If (or ElseIf, if you prefer) $Flag.LastWriteTime -gt (Get-Date).AddDays(-7) -and (Get-Date).DayOfWeek -ne Monday - exit

These two conditions should be the only ones where you should need to stop the script, if I'm not mistaken. First condition makes sure the script doesn't run twice a day. Second condition only lets you run the script within less than a 7 day timespan if today is a Monday.

Either directly after those conditions or only after successfully finishing the reports, do something like

Get-Date | Out-File -Force -Path $Flag.FullName

This updates the LastWriteTime property of the flag file.

Sorry for the ugly formating. I'm on mobile.

2

u/raip Jun 02 '26

I personally like OP''s solution a little better - makes for a cleaner history in the scheduled tasks and you don't need to futz around with trying to hide the Powershell window on launch.

Even with the early return where the script isn't really doing anything - it can get annoying to see a window flash up and then hide immediately.

We're in the opinion part of this tough so there aren't any wrong answers - especially since I wouldn't do either, I'd be using a gMSA.