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
27
u/OkRaspberry6530 Jun 02 '26
You can use a group managed service account which means no one has the credentials. The challenge is you need to first schedule the task, then update it using powershell to register it with the gmsa.
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.Â
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.
3
u/jeffrey_f Jun 02 '26
You only want it to run once, then put a datetime stamp into a file, read that file the next time around, and if the date is the same, don't run it. First run, create the file, datetime stamp, and run.........next time, compare date, if equal, exit
2
u/Modify- Jun 02 '26
While you could do this in pure Powershell.
What might be easier is to create what you want in the GUI, export it as XML so you have a template.
Then create a here string with that XML in Powershell
Use this to create a task: Register-ScheduledTask -Xml $XML -TaskName <taskname>
4
u/Deep-Trick7995 Jun 02 '26
Time to use gmsa !
1
u/omglazrgunpewpew Jun 03 '26
One thing I haven’t seen mentioned: if the actual restriction is no stored credentials, not literally that the only allowed trigger is At logon, you might be able to do this with a weekly Monday trigger,Â
StartWhenAvailable, and an interactive principal.Keeps it as a real weekly scheduled task, but still avoids storing creds because it only runs under your interactive logon token.Â
StartWhenAvailable is the bit that tells Task Scheduler it can start after the scheduled time was missed.If policy really only allowsÂ
At logon, then yourÂStartBoundary approach makes sense. I’d prob just addÂ-MultipleInstances IgnoreNew and maybe simplify the next Monday math, but the basic idea seems fine.2
u/BlackV Jun 02 '26
I don't think at login is a good trigger
Sounds like you want gmsa to run your tasks, that way it's a valid domain account and no credentials are stored
2
u/HelloImAbe Jun 02 '26
Someone said it already but I would emphasize on using the gMSA, as well. There's honestly no reason they shouldn't accept it.
1
u/Xydan Jun 02 '26
Sounds like you might need to start developing an actual batch job with a KMS that your boss agrees on. This way you're scheduling the task but the service itself is requesting a password it only has access too. If hes concerned about a token being accessed then you use OAuth. Might take you a weekend but truthfully this feels like a new standard in our industry
1
u/orbthatisfloating Jun 04 '26
our security team is always fucking with our task scheduler so we use a gmsa with sql agent jobs these days
0
u/Ok_Mathematician6075 Jun 03 '26
All the week shit.