Monday 12 November 2018

Create a Scheduled Task

The following PowerShell code was used to create a Scheduled Task for an application called Stata which needed to run an update file in its install directory each week. The snippet is part of a larger installation script.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
#Create an Action to run PowerShell and feed into it a specific file
$action = New-ScheduledTaskAction -Execute "$envSystem32Directory\WindowsPowerShell\v1.0\powershell.exe" -Argument "-File `"C:\Program Files (x86)\Stata15\run-update.ps1`""

#Create a Trigger for when the task should run
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek 'Monday' -At '9am'


#Combine the above and create the task, placed in a folder called 'IT Services', ran as an Administrator hidden from the logged-on user
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "Stata Updater" -TaskPath 'IT Services' -User 'System' -RunLevel 'Highest'

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Create a Scheduled Task.ps1

No comments:

Post a Comment