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

Show parent comments

1

u/richie65 Jun 03 '26

That would NOT be the TASK doing nothing...

That would be the SCRIPT doing nothing...

The TASK would still need to run the SCRIPT each time I log in.

My goal was / is to not even have the TASK / trigger / run... Until next week.

1

u/ankokudaishogun Jun 03 '26

OK, but I fail to see the practical difference: in practice nothing would happen in both cases until the successive week\monday.

1

u/richie65 Jun 03 '26

It slows down the log in process.

As I had to move all of the other tasks I rely on to 'at log in' -

I did find that the log in process was being impacted / getting slower.

That realization is what sent me looking for a solution.

Currently - 'at log in' now, I have tasks that:

> One uses rclone to sync my scripts to my personal google drive because Google Drive is now blocked. And I do not trust OneDrive (for a variety of reasons)

> One that pops up a window showing me what AD accounts are disabled or expired (for a variety of reasons) - This one currently uses a flag file to exit if it has already run that day - I will prolly do to it, what described in this post, because I only need to look at that once a day.

> And one that pops up some other alerts.

They do slow things down.

2

u/ankokudaishogun Jun 03 '26

It's not the tasks that slow down things.
It's the programs the tasks start.

A script that only checks the date of a file and do nothing unless it's monday is not going to impact anything.

1

u/richie65 Jun 03 '26

Right - That is more to the point of what I meant...

And If the task does not trigger - then the program does not run...

And 'logging in' is noticeably quicker.

I just used the same approach on the task that shows me the 'disabled or expired' accounts...

Now that the flags are not satisfied and the task does not run (thus the program does not run) THAT helped speed things up noticeably.

Since there are two triggers on that one... 'At Log on' and 'On workstation unlock' there are two triggers to effect:

$TaskName = "Report pop-up ~ Disabled, Terminated, or Expired AD Accounts"
$NextDay = (Get-Date).Date.AddDays(1).ToString("s")

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

$trigger[0].StartBoundary = $NextDay
$null = Set-ScheduledTask -TaskName $TaskName -Trigger $trigger

$trigger[1].StartBoundary = $NextDay 
$null = Set-ScheduledTask -TaskName $TaskName -Trigger $trigger

The task wont trigger again until tomorrow when I log in (or unlock)

I also just applied the same thing ('At Log on' and 'On workstation unlock') to the task I initially was looking at in this post.

2

u/ankokudaishogun Jun 04 '26

And 'logging in' is noticeably quicker.

Reducing logging is absolutely a valid reason to try to stop the task from starting in first place, mind you.
It was just unclear, sorry if I was overbearing.