Create scheduled task via PowerShell

183 views
Skip to first unread message

Dave Lum

unread,
Dec 20, 2024, 10:28:41 AM12/20/24
to ntsys...@googlegroups.com

Sounds easy, until two ingredients are added.

 

  1. Needs to use a gMSA account, which forces using PowerShell and not the UI (ok not a big deal really)
  2. I want it to run daily (so you’d think use the -Daily option) but also every hour for the next 16 hours (which means -RepetitionInterval, which is not available if using the -Daily option)

 

I have a task I want to start at 4am, run every hour using gMSA credentials, then stop at 8pm.  Rinse and repeat.

 

I found a Reddit post that says modify the task after registering it, but the way they formatted their code I didn’t understand it, but maybe that’s due to Friday brain.
https://www.reddit.com/r/PowerShell/comments/8vjpzq/registerscheduledtask_using_daily_and_repetition/

Dave

 

Attention: Information contained in this message and or attachments is intended only for the recipient(s) named above and may contain confidential and or privileged material that is protected under State or Federal law. If you are not the intended recipient, any disclosure, copying, distribution or action taken on it is prohibited. If you believe you have received this email in error, please contact the sender with a copy to compl...@ochin.org, delete this email and destroy all copies.

Michael B. Smith

unread,
Dec 20, 2024, 1:00:51 PM12/20/24
to ntsys...@googlegroups.com

I would schedule this to run every hour of every day and if it’s not between 4am and 8pm to terminate without doing anything.

--
You received this message because you are subscribed to the Google Groups "ntsysadmin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ntsysadmin+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/ntsysadmin/CO1PR17MB530871ABAF282431D43C2D76DD072%40CO1PR17MB5308.namprd17.prod.outlook.com.

uj...@pdx.edu

unread,
Dec 20, 2024, 4:37:13 PM12/20/24
to ntsysadmin
I have at least one of those - runs hourly M-F but only between 6:50am and 7:50pm, and uses gMSA. 
Create a -ONCE type repetition trigger and then copy the repetition part to the -Daily or -Weekly trigger object.
I keep these register job scripts in the script folder so I can change the parameters if needed and rerun to update the schedule.  This one is running scheduling a powershell script, but can adjust $action as needed.

RegisterScheduledJob.ps1

$ServiceAccount = "gMSAAccountName"
$TaskName = "TaskName"
$TaskPath = "Container\SubContainer"
$action = New-ScheduledTaskAction  "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "-noprofile -noninteractive -executionpolicy bypass -file `"$PSScriptRoot\Script.ps1`"" -WorkingDirectory $PSScriptRoot
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Monday,Tuesday,Wednesday,Thursday,Friday  -At 6:50am

#need this to add a repeat option to a create a repetition schedule to add to a weekly or daily trigger as only the ONCE takes -RepetitionInterval and -RepetitionDuration
$repeat_trigger = New-ScheduledTaskTrigger -once -At 6:50am -RepetitionInterval (New-TimeSpan -Hours 1) -RepetitionDuration (New-TimeSpan -hours 13)
$trigger.Repetition = $repeat_trigger.Repetition

$principal = New-ScheduledTaskPrincipal -UserID "Domain\$ServiceAccount`$" -LogonType Password -RunLevel Highest
$settings = New-ScheduledTaskSettingsSet -ExecutionTimeLimit (New-TimeSpan -Hours 2) -AllowStartIfOnBatteries:$true -Disable:$false
Register-ScheduledTask -TaskName $TaskName -Action $action -Trigger $trigger -Principal $principal -Settings $settings -TaskPath $TaskPath -Force

Dave Lum

unread,
Dec 24, 2024, 8:58:12 AM12/24/24
to ntsys...@googlegroups.com

Oh brilliant, thanks!

 

From: ntsys...@googlegroups.com <ntsys...@googlegroups.com> On Behalf Of uj...@pdx.edu
Sent: Friday, December 20, 2024 1:37 PM
To: ntsysadmin <ntsys...@googlegroups.com>
Subject: [ntsysadmin] Re: Create scheduled task via PowerShell

 

You don't often get email from uj...@pdx.edu. Learn why this is important

CAUTION: This email originated from outside of OCHIN’s network

Do not click links or open attachments unless you recognize the sender and know the content is safe. If you suspect this email is phishing or a scam, use the report button in the Outlook toolbar to report it to Desktop Support.

 

Reply all
Reply to author
Forward
0 new messages